L1TF Patch Series v8
This patch series attempts to mitigate the issue that have been raised in the XSA-289 (https://xenbits.xen.org/xsa/advisory-289.html). To block speculative execution on Intel hardware, an lfence instruction is required to make sure that selected checks are not bypassed. Speculative out-of-bound accesses can be prevented by using the array_index_nospec macro.
The major change compared to version 8 is in grant_table handling, protecting a few more version comparisons.
Best,
Norbert
Amazon Development Center Germany GmbH Krausenstr. 38
10117 Berlin
Geschaeftsfuehrer: Christian Schlaeger, Ralf Herbrich
Ust-ID: DE 289 237 879
Eingetragen am Amtsgericht Charlottenburg HRB 149173 B
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v9 1/7 spec: add l1tf-barrier
To control the runtime behavior on L1TF vulnerable platforms better, the command line option l1tf-barrier is introduced. This option controls whether on vulnerable x86 platforms the lfence instruction is used to prevent speculative execution from bypassing the evaluation of conditionals that are protected with the evaluate_nospec macro.
By now, Xen is capable of identifying L1TF vulnerable hardware. However, this information cannot be used for alternative patching, as a CPU feature is required. To control alternative patching with the command line option, a new x86 feature "X86_FEATURE_SC_L1TF_VULN" is introduced. This feature is used to patch the lfence instruction into the arch_barrier_nospec_true function. The feature is enabled only if L1TF vulnerable hardware is detected and the command line option does not prevent using this feature.
The status of hyperthreading is considered when automatically enabling adding the lfence instruction. Since platforms without hyperthreading can still be vulnerable to L1TF in case the L1 cache is not flushed properly, the additional lfence instructions are patched in if either hyperthreading is enabled, or L1 cache flushing is missing.
This is part of the speculative hardening effort.
--- docs/misc/xen-command-line.pandoc | 14 ++++++++++---- xen/arch/x86/spec_ctrl.c | 17 +++++++++++++++-- xen/include/asm-x86/cpufeatures.h | 1 + xen/include/asm-x86/spec_ctrl.h | 1 + 4 files changed, 27 insertions(+), 6 deletions(-)
docs/misc/xen-command-line.pandoc +10 -4
@@ -483,9 +483,9 @@ accounting for hardware capabilities as enumerated via CPUID. Currently accepted: -The Speculation Control hardware features `ibrsb`, `stibp`, `ibpb`, -`l1d-flush` and `ssbd` are used by default if available and applicable. They can -be ignored, e.g. `no-ibrsb`, at which point Xen won't use them itself, and +The Speculation Control hardware features `ibrsb`, `stibp`, `ibpb`, `l1d-flush`, +`l1tf-barrier` and `ssbd` are used by default if available and applicable. They +can be ignored, e.g. `no-ibrsb`, at which point Xen won't use them itself, and won't offer them to guests. ### cpuid_mask_cpu
@@ -1896,7 +1896,7 @@ By default SSBD will be mitigated at runtime (i.e `ssbd=runtime`). ### spec-ctrl (x86) > `= List of [ <bool>, xen=<bool>, {pv,hvm,msr-sc,rsb}=<bool>, > bti-thunk=retpoline|lfence|jmp, {ibrs,ibpb,ssbd,eager-fpu, -> l1d-flush}=<bool> ]` +> l1d-flush,l1tf-barrier}=<bool> ]` Controls for speculative execution sidechannel mitigations. By default, Xen will pick the most appropriate mitigations based on compiled in support,
@@ -1962,6 +1962,12 @@ Irrespective of Xen's setting, the feature is virtualised for HVM guests to use. By default, Xen will enable this mitigation on hardware believed to be vulnerable to L1TF. +On hardware vulnerable to L1TF, the `l1tf-barrier=` option can be used to force +or prevent Xen from protecting evaluations inside the hypervisor with a barrier +instruction to not load potentially secret information into L1 cache. By +default, Xen will enable this mitigation on hardware believed to be vulnerable +to L1TF. + ### sync_console > `= <boolean>`
xen/arch/x86/spec_ctrl.c +15 -2
@@ -21,6 +21,7 @@#include <xen/lib.h> #include <xen/warning.h> +#include <asm/cpuid.h> #include <asm/microcode.h> #include <asm/msr.h> #include <asm/processor.h>
@@ -50,6 +51,7 @@ bool __read_mostly opt_ibpb = true;bool __read_mostly opt_ssbd = false;int8_t __read_mostly opt_eager_fpu =-1 ;int8_t __read_mostly opt_l1d_flush =-1 ; +int8_t __read_mostly opt_l1tf_barrier =-1 ;bool __initdata bsp_delay_spec_ctrl;uint8_t __read_mostly default_xen_spec_ctrl;
@@ -91,6 +93,8 @@ static int __init parse_spec_ctrl(const char *s)if ( opt_pv_l1tf_domu <0 ) opt_pv_l1tf_domu =0 ; + opt_l1tf_barrier =0 ; + disable_common: opt_rsb_pv = false; opt_rsb_hvm = false;
@@ -157,6 +161,8 @@ static int __init parse_spec_ctrl(const char *s) opt_eager_fpu = val; else if ( (val = parse_boolean("l1d-flush", s, ss)) >= 0 ) opt_l1d_flush = val; + else if ( (val = parse_boolean("l1tf-barrier", s, ss)) >= 0 ) + opt_l1tf_barrier = val; else rc = -EINVAL ;
@@ -248,7 +254,7 @@ static void __init print_details(enum ind_thunk thunk, uint64_t caps) "\n");/* Settings for Xen's protection, irrespective of guests. */ -printk (" Xen settings: BTI-Thunk %s, SPEC_CTRL: %s%s, Other:%s%s\n" , +printk (" Xen settings: BTI-Thunk %s, SPEC_CTRL: %s%s, Other:%s%s%s\n" , thunk ==THUNK_NONE ?"N/A" : thunk ==THUNK_RETPOLINE ?"RETPOLINE" : thunk == THUNK_LFENCE ? "LFENCE" :
@@ -258,7 +264,8 @@ static void __init print_details(enum ind_thunk thunk, uint64_t caps) !boot_cpu_has (X86_FEATURE_SSBD ) ?"" : (default_xen_spec_ctrl &SPEC_CTRL_SSBD ) ?" SSBD+" :" SSBD-" , opt_ibpb ?" IBPB" :"" , - opt_l1d_flush ? " L1D_FLUSH" : ""); + opt_l1d_flush ?" L1D_FLUSH" :"" , + opt_l1tf_barrier ? " L1TF_BARRIER" : "");/* L1TF diagnostics, printed if vulnerable or PV shadowing is in use. */ if ( cpu_has_bug_l1tf || opt_pv_l1tf_hwdom || opt_pv_l1tf_domu )
@@ -842,6 +849,12 @@ void __init init_speculation_mitigations(void)else if ( opt_l1d_flush ==-1 ) opt_l1d_flush = cpu_has_bug_l1tf && !(caps &ARCH_CAPS_SKIP_L1DFL ); +/* By default, enable L1TF_VULN on L1TF-vulnerable hardware */ +if ( opt_l1tf_barrier ==-1 ) + opt_l1tf_barrier = cpu_has_bug_l1tf && (opt_smt || !opt_l1d_flush); +if ( opt_l1tf_barrier >0 ) +setup_force_cpu_cap (X86_FEATURE_SC_L1TF_VULN ); +/* * We do not disable HT by default on affected hardware. *
xen/include/asm-x86/cpufeatures.h +1 -0
@@ -25,6 +25,7 @@ XEN_CPUFEATURE(XEN_SMAP, (FSCAPINTS+0)*32+11) /* SMAP gets used by Xen it XEN_CPUFEATURE(LFENCE_DISPATCH, (FSCAPINTS+0)*32+12) /* lfence set as Dispatch Serialising */ XEN_CPUFEATURE(IND_THUNK_LFENCE,(FSCAPINTS+0)*32+13) /* Use IND_THUNK_LFENCE */ XEN_CPUFEATURE(IND_THUNK_JMP, (FSCAPINTS+0)*32+14) /* Use IND_THUNK_JMP */ +XEN_CPUFEATURE(SC_L1TF_VULN, (FSCAPINTS+0)*32+15) /* L1TF protection required */ XEN_CPUFEATURE(SC_MSR_PV, (FSCAPINTS+0)*32+16) /* MSR_SPEC_CTRL used by Xen for PV */ XEN_CPUFEATURE(SC_MSR_HVM, (FSCAPINTS+0)*32+17) /* MSR_SPEC_CTRL used by Xen for HVM */XEN_CPUFEATURE (SC_RSB_PV , (FSCAPINTS +0 )*32 +18 )/* RSB overwrite needed for PV */
xen/include/asm-x86/spec_ctrl.h +1 -0
@@ -37,6 +37,7 @@ extern bool opt_ibpb;extern bool opt_ssbd;extern int8_t opt_eager_fpu;extern int8_t opt_l1d_flush; +extern int8_t opt_l1tf_barrier;extern bool bsp_delay_spec_ctrl;extern uint8_t default_xen_spec_ctrl;
Signature · 18 lines
2.7.4 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrer: Christian Schlaeger, Ralf Herbrich Ust-ID: DE 289 237 879 Eingetragen am Amtsgericht Charlottenburg HRB 149173 B _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v9 2/7 nospec: introduce evaluate_nospec
In reply to Norbert MantheySince the L1TF vulnerability of Intel CPUs, loading hypervisor data into L1 cache is problematic, because when hyperthreading is used as well, a guest running on the sibling core can leak this potentially secret data.
To prevent these speculative accesses, we block speculation after accessing the domain property field by adding lfence instructions. This way, the CPU continues executing and loading data only once the condition is actually evaluated.
As this protection is typically used in if statements, the lfence has to come in a compatible way. Therefore, a function that returns true after an lfence instruction is introduced. To protect both branches after a conditional, an lfence instruction has to be added for the two branches. To be able to block speculation after several evaluations, the generic barrier macro block_speculation is also introduced.
As the L1TF vulnerability is only present on the x86 architecture, there is no need to add protection for other architectures. Hence, the introduced functions are defined but empty.
On the x86 architecture, by default, the lfence instruction is not present either. Only when a L1TF vulnerable platform is detected, the lfence instruction is patched in via alternative patching. Similarly, PV guests are protected wrt L1TF by default, so that the protection is furthermore disabled in case HVM is exclueded via the build configuration.
Introducing the lfence instructions catches a lot of potential leaks with a simple unintrusive code change. During performance testing, we did not notice performance effects.
This is part of the speculative hardening effort.
---
Notes:
v9: fixed indentation (ARM)
dropped CONFIG_HVM in evaluate_nospec
dropped cast in block_speculation
xen/include/asm-arm/nospec.h | 25 +++++++++++++++++++++++++ xen/include/asm-x86/nospec.h | 39 +++++++++++++++++++++++++++++++++++++++ xen/include/xen/nospec.h | 1 + 3 files changed, 65 insertions(+) create mode 100644 xen/include/asm-arm/nospec.h create mode 100644 xen/include/asm-x86/nospec.h
xen/include/asm-arm/nospec.h new file +25 -0
@@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. */ + +#ifndef _ASM_ARM_NOSPEC_H +#define _ASM_ARM_NOSPEC_H + +static inline bool evaluate_nospec (bool condition) +{ +return condition; +} + +static inline void block_speculation (void ) +{ +} + +#endif /* _ASM_ARM_NOSPEC_H */ + +/* +* Local variables: +* mode: C +* c-file-style: "BSD" +* c-basic-offset: 4 +* indent-tabs-mode: nil +* End: +*/
xen/include/asm-x86/nospec.h new file +39 -0
@@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. */ + +#ifndef _ASM_X86_NOSPEC_H +#define _ASM_X86_NOSPEC_H + +#include <asm/alternative.h> + +/* Allow to insert a read memory barrier into conditionals */ +static always_inline bool barrier_nospec_true(void) +{ +#ifdef CONFIG_HVM +alternative ("" ,"lfence" ,X86_FEATURE_SC_L1TF_VULN ); +#endif +return true; +} + +/* Allow to protect evaluation of conditionasl with respect to speculation */ +static always_inline bool evaluate_nospec(bool condition) +{ +return condition ?barrier_nospec_true () : !barrier_nospec_true (); +} + +/* Allow to block speculative execution in generic code */ +static always_inline void block_speculation(void) +{ +barrier_nospec_true (); +} + +#endif /* _ASM_X86_NOSPEC_H */ + +/* +* Local variables: +* mode: C +* c-file-style: "BSD" +* c-basic-offset: 4 +* indent-tabs-mode: nil +* End: +*/
xen/include/xen/nospec.h +1 -0
@@ -8,6 +8,7 @@#define XEN_NOSPEC_H #include <asm/system.h> +#include <asm/nospec.h> /** * array_index_mask_nospec() - generate a ~0 mask when index < size, 0 otherwise
Signature · 18 lines
2.7.4 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrer: Christian Schlaeger, Ralf Herbrich Ust-ID: DE 289 237 879 Eingetragen am Amtsgericht Charlottenburg HRB 149173 B _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH L1TF v9 2/7] nospec: introduce evaluate_nospec
Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 27.02.19 at 17:13, <[email protected]> wrote:
Since the L1TF vulnerability of Intel CPUs, loading hypervisor data into L1 cache is problematic, because when hyperthreading is used as well, a guest running on the sibling core can leak this potentially secret data.
To prevent these speculative accesses, we block speculation after accessing the domain property field by adding lfence instructions. This way, the CPU continues executing and loading data only once the condition is actually evaluated.
As this protection is typically used in if statements, the lfence has to come in a compatible way. Therefore, a function that returns true after an lfence instruction is introduced. To protect both branches after a conditional, an lfence instruction has to be added for the two branches. To be able to block speculation after several evaluations, the generic barrier macro
block_speculationis also introduced.As the L1TF vulnerability is only present on the x86 architecture, there is no need to add protection for other architectures. Hence, the introduced functions are defined but empty.
On the x86 architecture, by default, the lfence instruction is not present either. Only when a L1TF vulnerable platform is detected, the lfence instruction is patched in via alternative patching. Similarly, PV guests are protected wrt L1TF by default, so that the protection is furthermore disabled in case HVM is exclueded via the build configuration.
Quoted 8 lines
Introducing the lfence instructions catches a lot of potential leaks with a simple unintrusive code change. During performance testing, we did not notice performance effects.
This is part of the speculative hardening effort.
Signed-off-by Norbert Manthey <[email protected]>Acked-by Julien Grall <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v9 3/7 is_control_domain: block speculation
In reply to Norbert MantheyChecks of domain properties, such as is_hardware_domain or is_hvm_domain, might be bypassed by speculatively executing these instructions. A reason for bypassing these checks is that these macros access the domain structure via a pointer, and check a certain field. Since this memory access is slow, the CPU assumes a returned value and continues the execution.
In case an is_control_domain check is bypassed, for example during a hypercall, data that should only be accessible by the control domain could be loaded into the cache.
This is part of the speculative hardening effort.
--- xen/include/xen/sched.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
xen/include/xen/sched.h +2 -2
@@ -913,10 +913,10 @@ void watchdog_domain_destroy(struct domain *d);* (that is, this would not be suitable for a driver domain) * - There is never a reason to deny the hardware domain access to this */ -#define is_hardware_domain (_d) ((_d) == hardware_domain) +#define is_hardware_domain (_d) evaluate_nospec((_d) == hardware_domain)/* This check is for functionality specific to a control domain */ -#define is_control_domain (_d) ((_d)->is_privileged) +#define is_control_domain (_d) evaluate_nospec((_d)->is_privileged)#define VM_ASSIST (d, t) (test_bit(VMASST_TYPE_ ## t, &(d)->vm_assist))
Signature · 18 lines
2.7.4 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrer: Christian Schlaeger, Ralf Herbrich Ust-ID: DE 289 237 879 Eingetragen am Amtsgericht Charlottenburg HRB 149173 B _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v9 4/7 is_hvm/pv_domain: block speculation
In reply to Norbert MantheyWhen checking for being an hvm domain, or PV domain, we have to make sure that speculation cannot bypass that check, and eventually access data that should not end up in cache for the current domain type.
This is part of the speculative hardening effort.
--- xen/include/xen/sched.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
xen/include/xen/sched.h +4 -2
@@ -922,7 +922,8 @@ void watchdog_domain_destroy(struct domain *d);static inline bool is_pv_domain (const struct domain *d) { -return IS_ENABLED (CONFIG_PV ) ? d->guest_type == guest_type_pv : false; +return IS_ENABLED (CONFIG_PV ) + ?evaluate_nospec (d->guest_type == guest_type_pv) : false; }static inline bool is_pv_vcpu (const struct vcpu *v)
@@ -953,7 +954,8 @@ static inline bool is_pv_64bit_vcpu(const struct vcpu *v)#endif static inline bool is_hvm_domain (const struct domain *d) { -return IS_ENABLED (CONFIG_HVM ) ? d->guest_type == guest_type_hvm : false; +return IS_ENABLED (CONFIG_HVM ) + ?evaluate_nospec (d->guest_type == guest_type_hvm) : false; }static inline bool is_hvm_vcpu (const struct vcpu *v)
Signature · 18 lines
2.7.4 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrer: Christian Schlaeger, Ralf Herbrich Ust-ID: DE 289 237 879 Eingetragen am Amtsgericht Charlottenburg HRB 149173 B _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v9 5/7 common/memory: block speculative out-of-bound accesses
In reply to Norbert MantheyThe get_page_from_gfn method returns a pointer to a page that belongs to a gfn. Before returning the pointer, the gfn is checked for being valid. Under speculation, these checks can be bypassed, so that the function get_page is still executed partially. Consequently, the function page_get_owner_and_reference might be executed partially as well. In this function, the computed pointer is accessed, resulting in a speculative out-of-bound address load. As the gfn can be controlled by a guest, this access is problematic.
To mitigate the root cause, an lfence instruction is added via the evaluate_nospec macro. To make the protection generic, we do not introduce the lfence instruction for this single check, but add it to the mfn_valid function. This way, other potentially problematic accesses are protected as well.
This is part of the speculative hardening effort.
--- xen/common/pdx.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
xen/common/pdx.c +4 -2
@@ -18,6 +18,7 @@#include <xen/init.h> #include <xen/mm.h> #include <xen/bitops.h> +#include <xen/nospec.h> /* Parameters for PFN/MADDR compression. */ unsigned long __read_mostly max_pdx;
@@ -33,8 +34,9 @@ unsigned long __read_mostly pdx_group_valid[BITS_TO_LONGS(bool __mfn_valid (unsigned long mfn) { -return likely (mfn < max_page) && -likely (!(mfn & pfn_hole_mask)) && +if (unlikely (evaluate_nospec (mfn >= max_page)) ) +return false; +return likely (!(mfn & pfn_hole_mask)) &&likely (test_bit (pfn_to_pdx (mfn) /PDX_GROUP_COUNT , pdx_group_valid)); }
Signature · 18 lines
2.7.4 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrer: Christian Schlaeger, Ralf Herbrich Ust-ID: DE 289 237 879 Eingetragen am Amtsgericht Charlottenburg HRB 149173 B _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v9 6/7 x86/hvm: add nospec to hvmop param
In reply to Norbert MantheyThe params array in hvm can be accessed with get and set functions. As the index is guest controlled, make sure no out-of-bound accesses can be performed.
As we cannot influence how future compilers might modify the instructions that enforce the bounds, we furthermore block speculation, so that the update is visible in the architectural state.
This is part of the speculative hardening effort.
---
Notes:
v9: fixed inline comments
added acked-by
xen/arch/x86/hvm/hvm.c | 6 ++++++ 1 file changed, 6 insertions(+)
xen/arch/x86/hvm/hvm.c +6 -0
@@ -4135,6 +4135,9 @@ static int hvmop_set_param(if ( a.index >=HVM_NR_PARAMS )return -EINVAL ; +/* Make sure the above bound check is not bypassed during speculation. */ +block_speculation (); + d =rcu_lock_domain_by_any_id (a.domid);if ( d ==NULL )return -ESRCH ;
@@ -4401,6 +4404,9 @@ static int hvmop_get_param(if ( a.index >=HVM_NR_PARAMS )return -EINVAL ; +/* Make sure the above bound check is not bypassed during speculation. */ +block_speculation (); + d =rcu_lock_domain_by_any_id (a.domid);if ( d ==NULL )return -ESRCH ;
Signature · 18 lines
2.7.4 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrer: Christian Schlaeger, Ralf Herbrich Ust-ID: DE 289 237 879 Eingetragen am Amtsgericht Charlottenburg HRB 149173 B _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v9 7/7 common/grant_table: block speculative out-of-bound accesses
In reply to Norbert MantheyGuests can issue grant table operations and provide guest controlled data to them. This data is also used for memory loads. To avoid speculative out-of-bound accesses, we use the array_index_nospec macro where applicable. However, there are also memory accesses that cannot be protected by a single array protection, or multiple accesses in a row. To protect these, a nospec barrier is placed between the actual range check and the access via the block_speculation macro.
As different versions of grant tables use structures of different size, and the status is encoded in an array for version 2, speculative execution might perform out-of-bound accesses of version 2 while the table is actually using version 1. Hence, speculation is prevented when accessing memory based on the grant table version.
Speculative execution is not blocked in case one of the following properties is true:
- path cannot be triggered by the guest
- path does not return to the guest
- path does not result in an out-of-bound access
- path cannot be executed repeatedly
Only the combination of the above properties allows to actually leak continuous chunks of memory. Therefore, we only add the penalty of protective mechanisms in case a potential speculative out-of-bound access matches all the above properties.
This is part of the speculative hardening effort.
---
Notes:
v8: extended commit message with reason when to block speculation
fix order assert_unreachable and block_speculation
add block_speculation to gnttab_get_status_frame_mfn
protect version comparison in gnttab_map_frame and release_grant_for_copy
xen/common/grant_table.c | 96 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 74 insertions(+), 22 deletions(-)
xen/common/grant_table.c +74 -22
@@ -37,6 +37,7 @@#include <xen/paging.h> #include <xen/keyhandler.h> #include <xen/vmap.h> +#include <xen/nospec.h> #include <xsm/xsm.h> #include <asm/flushtlb.h>
@@ -203,8 +204,9 @@ static inline unsigned int nr_status_frames(const struct grant_table *gt) }#define MAPTRACK_PER_PAGE (PAGE_SIZE / sizeof(struct grant_mapping)) -#define maptrack_entry (t, e) \ - ((t)->maptrack[(e)/MAPTRACK_PER_PAGE][(e)%MAPTRACK_PER_PAGE]) +#define maptrack_entry (t, e) \ + ((t)->maptrack[array_index_nospec(e, (t)->maptrack_limit) / \ + MAPTRACK_PER_PAGE][(e) % MAPTRACK_PER_PAGE])static inline unsigned int nr_maptrack_frames (struct grant_table *t)
@@ -226,10 +228,23 @@ nr_maptrack_frames(struct grant_table *t)static grant_entry_header_t *shared_entry_header (struct grant_table *t,grant_ref_t ref) { -if ( t->gt_version ==1 ) +switch ( t->gt_version ) + { +case 1 : +/* Returned values should be independent of speculative execution */ +block_speculation ();return (grant_entry_header_t *)&shared_entry_v1 (t, ref); -else + +case 2 : +/* Returned values should be independent of speculative execution */ +block_speculation ();return &shared_entry_v2 (t, ref).hdr; + } + +ASSERT_UNREACHABLE (); +block_speculation (); + +return NULL ; }/* Active grant entry - used for shadowing GTF_permit_access grants. */
@@ -634,14 +649,24 @@ static unsigned int nr_grant_entries(struct grant_table *gt)case 1 :BUILD_BUG_ON (f2e (INITIAL_NR_GRANT_FRAMES ,1 ) <GNTTAB_NR_RESERVED_ENTRIES ); + +/* Make sure we return a value independently of speculative execution */ +block_speculation ();return f2e (nr_grant_frames (gt),1 ); +case 2 :BUILD_BUG_ON (f2e (INITIAL_NR_GRANT_FRAMES ,2 ) <GNTTAB_NR_RESERVED_ENTRIES ); + +/* Make sure we return a value independently of speculative execution */ +block_speculation ();return f2e (nr_grant_frames (gt),2 );#undef f2e } +ASSERT_UNREACHABLE (); +block_speculation (); +return 0 ; }
@@ -963,9 +988,13 @@ map_grant_ref(PIN_FAIL (unlock_out, GNTST_bad_gntref,"Bad ref %#x for d%d\n" , op->ref, rgt->domain->domain_id); - act =active_entry_acquire (rgt, op->ref); +/* This call ensures the above check cannot be bypassed speculatively */ shah =shared_entry_header (rgt, op->ref); - status = rgt->gt_version ==1 ? &shah->flags : &status_entry (rgt, op->ref); + act =active_entry_acquire (rgt, op->ref); + +/* Make sure we do not access memory speculatively */ + status =evaluate_nospec (rgt->gt_version ==1 ) ? &shah->flags + : &status_entry (rgt, op->ref);/* If already pinned, check the active domid and avoid refcnt overflow. */ if ( act->pin &&
@@ -987,7 +1016,7 @@ map_grant_ref(if ( !act->pin ) { -unsigned long gfn = rgt->gt_version ==1 ? +unsigned long gfn =evaluate_nospec (rgt->gt_version ==1 ) ?shared_entry_v1 (rgt, op->ref).frame :shared_entry_v2 (rgt, op->ref).full_page.frame;
@@ -1321,6 +1350,9 @@ unmap_common( goto unlock_out; } +/* Make sure the above bound check cannot be bypassed speculatively */ +block_speculation (); + act =active_entry_acquire (rgt, op->ref);/*
@@ -1418,7 +1450,7 @@ unmap_common_complete(struct gnttab_unmap_common *op)struct page_info *pg;uint16_t *status; -if ( !op->done ) +if (evaluate_nospec (!op->done) ) {/* unmap_common() didn't do anything - nothing to complete. */ return ;
@@ -2026,6 +2058,7 @@ gnttab_prepare_for_transfer( goto fail; } +/* This call ensures the above check cannot be bypassed speculatively */ sha =shared_entry_header (rgt, ref); scombo.word = *(u32 *)&sha->flags;
@@ -2223,7 +2256,11 @@ gnttab_transfer( okay =gnttab_prepare_for_transfer (e, d, gop.ref);spin_lock (&e->page_alloc_lock); -if (unlikely (!okay) ||unlikely (e->is_dying) ) +/* +* Make sure the reference bound check in gnttab_prepare_for_transfer +* is respected and speculative execution is blocked accordingly +*/ +if (unlikely (!evaluate_nospec (okay)) ||unlikely (e->is_dying) ) {bool_t drop_dom_ref = !domain_adjust_tot_pages (e,-1 );
@@ -2253,7 +2290,7 @@ gnttab_transfer(grant_read_lock (e->grant_table); act =active_entry_acquire (e->grant_table, gop.ref); -if ( e->grant_table->gt_version ==1 ) +if (evaluate_nospec (e->grant_table->gt_version ==1 ) ) {grant_entry_v1_t *sha = &shared_entry_v1 (e->grant_table, gop.ref);
@@ -2314,7 +2351,7 @@ release_grant_for_copy( sha =shared_entry_header (rgt, gref); mfn = act->mfn; -if ( rgt->gt_version ==1 ) +if (evaluate_nospec (rgt->gt_version ==1 ) ) { status = &sha->flags; td = rd;
@@ -2408,9 +2445,11 @@ acquire_grant_for_copy(PIN_FAIL (gt_unlock_out, GNTST_bad_gntref,"Bad grant reference %#x\n" , gref); - act =active_entry_acquire (rgt, gref); +/* This call ensures the above check cannot be bypassed speculatively */ shah =shared_entry_header (rgt, gref); -if ( rgt->gt_version ==1 ) + act =active_entry_acquire (rgt, gref); + +if (evaluate_nospec (rgt->gt_version ==1 ) ) { sha2 =NULL ; status = &shah->flags;
@@ -2826,6 +2865,9 @@ static int gnttab_copy_buf(const struct gnttab_copy *op, op->dest.offset, dest->ptr.offset, op->len, dest->len); +/* Make sure the above checks are not bypassed speculatively */ +block_speculation (); +memcpy (dest->virt + op->dest.offset, src->virt + op->source.offset, op->len);gnttab_mark_dirty (dest->domain, dest->mfn);
@@ -2945,7 +2987,7 @@ gnttab_set_version(XEN_GUEST_HANDLE_PARAM(gnttab_set_version_t) uop)struct grant_table *gt = currd->grant_table;grant_entry_v1_t reserved_entries[GNTTAB_NR_RESERVED_ENTRIES ];int res; -unsigned int i; +unsigned int i, nr_ents;if (copy_from_guest (&op, uop,1 ) )return -EFAULT ;
@@ -2969,7 +3011,8 @@ gnttab_set_version(XEN_GUEST_HANDLE_PARAM(gnttab_set_version_t) uop)* are allowed to be in use (xenstore/xenconsole keeps them mapped). * (You need to change the version number for e.g. kexec.) */ -for ( i =GNTTAB_NR_RESERVED_ENTRIES ; i <nr_grant_entries (gt); i++ ) + nr_ents =nr_grant_entries (gt); +for ( i =GNTTAB_NR_RESERVED_ENTRIES ; i < nr_ents; i++ ) {if (read_atomic (&_active_entry (gt, i).pin) !=0 ) {
@@ -3211,6 +3254,9 @@ swap_grant_ref(grant_ref_t ref_a, grant_ref_t ref_b)if (unlikely (ref_b >=nr_grant_entries (d->grant_table)))PIN_FAIL (out, GNTST_bad_gntref,"Bad ref-b %#x\n" , ref_b); +/* Make sure the above checks are not bypassed speculatively */ +block_speculation (); +/* Swapping the same ref is a no-op. */ if ( ref_a == ref_b ) goto out;
@@ -3223,7 +3269,7 @@ swap_grant_ref(grant_ref_t ref_a, grant_ref_t ref_b)if ( act_b->pin )PIN_FAIL (out, GNTST_eagain,"ref b %#x busy\n" , ref_b); -if ( gt->gt_version ==1 ) +if (evaluate_nospec (gt->gt_version ==1 ) ) {grant_entry_v1_t shared;
@@ -3680,13 +3726,14 @@ void grant_table_warn_active_grants(struct domain *d)struct grant_table *gt = d->grant_table;struct active_grant_entry *act;grant_ref_t ref; -unsigned int nr_active =0 ; +unsigned int nr_active =0 , nr_ents;#define WARN_GRANT_MAX 10grant_read_lock (gt); -for ( ref =0 ; ref !=nr_grant_entries (gt); ref++ ) + nr_ents =nr_grant_entries (gt); +for ( ref =0 ; ref != nr_ents; ref++ ) { act =active_entry_acquire (gt, ref);if ( !act->pin )
@@ -3771,7 +3818,7 @@ int mem_sharing_gref_to_gfn(struct grant_table *gt, grant_ref_t ref, rc = -EINVAL ; else if ( ref >= nr_grant_entries(gt) ) rc = -ENOENT ; - else if ( gt->gt_version == 1 ) + else if ( evaluate_nospec(gt->gt_version == 1) ) {const grant_entry_v1_t *sha1 = &shared_entry_v1 (gt, ref);
@@ -3793,7 +3840,7 @@ int mem_sharing_gref_to_gfn(struct grant_table *gt, grant_ref_t ref, rc = -ENXIO ; else if ( !rc && status ) { -if ( gt->gt_version ==1 ) +if (evaluate_nospec (gt->gt_version ==1 ) ) *status = flags;else *status =status_entry (gt, ref);
@@ -3836,6 +3883,9 @@ static int gnttab_get_status_frame_mfn(struct domain *d,return -EINVAL ; } +/* Make sure idx is bounded wrt nr_status_frames */ +block_speculation (); + *mfn =_mfn (virt_to_mfn (gt->status[idx]));return 0 ; }
@@ -3877,7 +3927,7 @@ int gnttab_map_frame(struct domain *d, unsigned long idx, gfn_t gfn, mfn_t *mfn)grant_write_lock (gt); -if ( gt->gt_version ==2 && (idx & XENMAPIDX_grant_table_status) ) +if (evaluate_nospec (gt->gt_version ==2 ) && (idx & XENMAPIDX_grant_table_status) ) { idx &= ~XENMAPIDX_grant_table_status; status = true;
@@ -3935,6 +3985,7 @@ static void gnttab_usage_print(struct domain *rd)int first =1 ;grant_ref_t ref;struct grant_table *gt = rd->grant_table; +unsigned int nr_ents;printk (" -------- active -------- -------- shared --------\n" );printk ("[ref] localdom mfn pin localdom gmfn flags\n" );
@@ -3947,7 +3998,8 @@ static void gnttab_usage_print(struct domain *rd)nr_grant_frames (gt), gt->max_grant_frames, nr_maptrack_frames(gt), gt->max_maptrack_frames); -for ( ref =0 ; ref !=nr_grant_entries (gt); ref++ ) + nr_ents =nr_grant_entries (gt); +for ( ref =0 ; ref != nr_ents; ref++ ) {struct active_grant_entry *act;struct grant_entry_header *sha;
Signature · 18 lines
2.7.4 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrer: Christian Schlaeger, Ralf Herbrich Ust-ID: DE 289 237 879 Eingetragen am Amtsgericht Charlottenburg HRB 149173 B _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH L1TF v9 7/7] common/grant_table: block speculative out-of-bound accesses
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 27.02.19 at 17:13, <[email protected]> wrote:
Speculative execution is not blocked in case one of the following properties is true:
- path cannot be triggered by the guest
- path does not return to the guest
- path does not result in an out-of-bound access
- path cannot be executed repeatedly
Only the combination of the above properties allows to actually leak continuous chunks of memory. Therefore, we only add the penalty of protective mechanisms in case a potential speculative out-of-bound access matches all the above properties.
While this is all fine, how do I match which of the reasons applies to which of (in particular) the gt_version checks left alone? As said, the reasoning here should specifically be detailed so it can be used as a guiding reference when adding further conditionals to the code down the road. And of course review is (more) difficult this way as well, as (judging from prior conversations) we don't seem to necessarily agree in our views in all places, and hence to discuss a possibly questionable decision others also need to understand which of the criteria you considered to match in the specific case.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH L1TF v9 7/7] common/grant_table: block speculative out-of-bound accesses
Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 27.02.19 at 17:13, <[email protected]> wrote:
Speculative execution is not blocked in case one of the following properties is true:
- path cannot be triggered by the guest
- path does not return to the guest
- path does not result in an out-of-bound access
- path cannot be executed repeatedly
Only the combination of the above properties allows to actually leak continuous chunks of memory. Therefore, we only add the penalty of protective mechanisms in case a potential speculative out-of-bound access matches all the above properties.
Quoted 9 lines
While this is all fine, how do I match which of the reasons applies to which of (in particular) the
gt_versionchecks left alone? As said, the reasoning here should specifically be detailed so it can be used as a guiding reference when adding further conditionals to the code down the road. And of course review is (more) difficult this way as well, as (judging from prior conversations) we don't seem to necessarily agree in our views in all places, and hence to discuss a possibly questionable decision others also need to understand which of the criteria you considered to match in the specific case.
I listed the reasons to use above (and in the commit message). I will extend the commit message and give a reason for each version comparison, similarly to a prior email I sent.
In my opinion, the commit message for fixing the problems we found in that file should not be a tutorial on how to identify and fix potential speculative out-of-bound accesses. While I agree that teaching more people how to judge whether a certain piece of code might lead to information leak via speculative execution, I would not use a commit message to get that covered.
Best,
Norbert
Quoted 4 lines
Jan
Amazon Development Center Germany GmbH Krausenstr. 38
10117 Berlin
Geschaeftsfuehrer: Christian Schlaeger, Ralf Herbrich
Ust-ID: DE 289 237 879
Eingetragen am Amtsgericht Charlottenburg HRB 149173 B
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH L1TF v9 7/7] common/grant_table: block speculative out-of-bound accesses
Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 12.03.19 at 11:36, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 27.02.19 at 17:13, <[email protected]> wrote:
Speculative execution is not blocked in case one of the following properties is true:
- path cannot be triggered by the guest
- path does not return to the guest
- path does not result in an out-of-bound access
- path cannot be executed repeatedly
Only the combination of the above properties allows to actually leak continuous chunks of memory. Therefore, we only add the penalty of protective mechanisms in case a potential speculative out-of-bound access matches all the above properties.Quoted 9 lines
While this is all fine, how do I match which of the reasons applies to which of (in particular) the
gt_versionchecks left alone? As said, the reasoning here should specifically be detailed so it can be used as a guiding reference when adding further conditionals to the code down the road. And of course review is (more) difficult this way as well, as (judging from prior conversations) we don't seem to necessarily agree in our views in all places, and hence to discuss a possibly questionable decision others also need to understand which of the criteria you considered to match in the specific case.I listed the reasons to use above (and in the commit message). I will extend the commit message and give a reason for each version comparison, similarly to a prior email I sent.
Quoted 6 lines
In my opinion, the commit message for fixing the problems we found in that file should not be a tutorial on how to identify and fix potential speculative out-of-bound accesses. While I agree that teaching more people how to judge whether a certain piece of code might lead to information leak via speculative execution, I would not use a commit message to get that covered.
And that also wasn't my line of argumentation. All I'm after is as good of a guideline as possible for how to deal with future new version dependent code in grant_table.c.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 27.02.19 at 17:13, <[email protected]> wrote:
This patch series attempts to mitigate the issue that have been raised in the XSA-289 (https://xenbits.xen.org/xsa/advisory-289.html). To block speculative execution on Intel hardware, an lfence instruction is required to make sure that selected checks are not bypassed. Speculative out-of-bound accesses can be prevented by using the
array_index_nospecmacro.The major change compared to version 8 is in
grant_tablehandling, protecting a few more version comparisons.
Apart from that last patch this series looks to have been ready to go in for about a week. Would you still want to allow it in, or rather defer it until after the release?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 27.02.19 at 17:13, <[email protected]> wrote:
This patch series attempts to mitigate the issue that have been raised in the XSA-289 (https://xenbits.xen.org/xsa/advisory-289.html). To block speculative execution on Intel hardware, an lfence instruction is required to make sure that selected checks are not bypassed. Speculative out-of-bound accesses can be prevented by using the
array_index_nospecmacro.
Quoted 6 lines
Quoted 2 lines
The major change compared to version 8 is in
grant_tablehandling, protecting a few more version comparisons.Apart from that last patch this series looks to have been ready to go in for about a week. Would you still want to allow it in, or rather defer it until after the release?
I'd like to defer them.
Juergen
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel