xen/sched: Introduce domain_vcpu() helper
PATCH 1/1 xen/sched: Introduce domain_vcpu() helper
The progression of multi-vcpu support in Xen (originally a single pointer, then an embedded d->vcpu[] array, then a dynamically allocated array) has resulted in a large quantity of ad-hoc code for looking a vcpu up by id, and a large number of ways that the toolstack can cause Xen to trip over a NULL pointer. Some of this has been addressed in Xen 4.12, and work is ongoing.
Another property of looking a vcpu up by id is frequently done in unprivileged hypercall context, making it an attractive target for speculative sidechannel attacks.
Introduce a helper to do the lookup correctly, and without speculative interference. For performance reasons, it is useful not to have an smp_rmb() in this helper on ARM, and luckily this is safe to do, because of the serialisation offered by the global domheap lock.
As a minor change noticed when checking the safety of this construct, sanity check during boot that idle->max_vcpus is a suitable upper bound for idle->vcpu[].
---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Norbert Manthey <[email protected]>
Some notes:
* This patch really is from 2014, and formed my first attempt to fix the NULL dereference problem, and eventally started getting upstream as the domain creation changes. It is posted now to help with the XSA-289 series.
* The reasoning about the safey of not having an smp_rmb() is only true for Xen 4.12. It does not hold for older versions of Xen.
--- xen/common/schedule.c | 1 + xen/include/xen/sched.h | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+)
xen/common/schedule.c +1 -0
@@ -1815,6 +1815,7 @@ void __init scheduler_init(void) idle_domain =domain_create (DOMID_IDLE ,NULL , false);BUG_ON (IS_ERR (idle_domain)); +BUG_ON (nr_cpu_ids >ARRAY_SIZE (idle_vcpu)); idle_domain->vcpu = idle_vcpu; idle_domain->max_vcpus = nr_cpu_ids;if (vcpu_create (idle_domain,0 ,0 ) ==NULL )
xen/include/xen/sched.h +26 -0
@@ -15,6 +15,7 @@#include <xen/nodemask.h> #include <xen/radix-tree.h> #include <xen/multicall.h> +#include <xen/nospec.h> #include <xen/tasklet.h> #include <xen/mm.h> #include <xen/smp.h>
@@ -836,6 +837,31 @@ static inline int domain_pause_by_systemcontroller_nosync(struct domain *d)void domain_pause_except_self (struct domain *d);void domain_unpause_except_self (struct domain *d); +/* +* For each allocated vcpu, d->vcpu[X]->vcpu_id == X +* +* During construction, all vcpus in d->vcpu[] are allocated sequentially, and +* in ascending order. Therefore, if d->vcpu[N] exists (e.g. derived from +* current), all vcpus with an id less than N also exist. +* +* SMP considerations: The idle domain is constructed before APs are started. +* All other domains have d->vcpu[] allocated and d->max_vcpus set before the +* domain is made visible in the domlist, which is serialised on the global +* domlist_update_lock. +* +* Therefore, all observations of d->max_vcpus vs d->vcpu[] will be consistent +* despite the lack of smp_* barriers, either by being on the same CPU as the +* one which issued the writes, or because of barrier properties of the domain +* having been inserted into the domlist. +*/ +static inline struct vcpu *domain_vcpu (const struct domain *d, +unsigned int vcpu_id) +{ +unsigned int idx =array_index_nospec (vcpu_id, d->max_vcpus); + +return idx >= d->max_vcpus ?NULL : d->vcpu[idx]; +} +void cpu_init (void );struct scheduler ;
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 20 lines · Andrew Cooper
The progression of multi-vcpu support in Xen (originally a single pointer, then an embedded d->vcpu[] array, then a dynamically allocated array) has resulted in a large quantity of ad-hoc code for looking a vcpu up by id, and a large number of ways that the toolstack can cause Xen to trip over a NULL pointer. Some of this has been addressed in Xen 4.12, and work is ongoing.
Another property of looking a vcpu up by id is frequently done in unprivileged hypercall context, making it an attractive target for speculative sidechannel attacks.
Introduce a helper to do the lookup correctly, and without speculative interference. For performance reasons, it is useful not to have an
smp_rmb()in this helper on ARM, and luckily this is safe to do, because of the serialisation offered by the global domheap lock.As a minor change noticed when checking the safety of this construct, sanity check during boot that
idle->max_vcpusis a suitable upper bound for idle->vcpu[].Signed-off-by Andrew Cooper <[email protected]>
Quoted 8 lines
---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Norbert Manthey <[email protected]>
And in my haste, I forgot to tag this as "for 4.12".
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 15 lines · Andrew Cooper
The progression of multi-vcpu support in Xen (originally a single pointer, then an embedded d->vcpu[] array, then a dynamically allocated array) has resulted in a large quantity of ad-hoc code for looking a vcpu up by id, and a large number of ways that the toolstack can cause Xen to trip over a NULL pointer. Some of this has been addressed in Xen 4.12, and work is ongoing.
Another property of looking a vcpu up by id is frequently done in unprivileged hypercall context, making it an attractive target for speculative sidechannel attacks.
Introduce a helper to do the lookup correctly, and without speculative interference. For performance reasons, it is useful not to have an
smp_rmb()in this helper on ARM, and luckily this is safe to do, because of the serialisation offered by the global domheap lock.
Quoted 5 lines
As a minor change noticed when checking the safety of this construct, sanity check during boot that
idle->max_vcpusis a suitable upper bound for idle->vcpu[].Signed-off-by Andrew Cooper <[email protected]>
Juergen
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
xen/include/xen/sched.h static inline int domain_pause_by_systemcontroller_nosync(struct domain *d) · line 863
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 23.01.19 at 15:59, <[email protected]> wrote:
xen/include/xen/sched.h +7
-0+static inline struct vcpu *domain_vcpu (const struct domain *d, +unsigned int vcpu_id) +{ +unsigned int idx =array_index_nospec (vcpu_id, d->max_vcpus); + +return idx >= d->max_vcpus ?NULL : d->vcpu[idx]; +}
For an out of bounds incoming vcpu_id, isn't it the case that idx then would be zero? In which case you'd return d->vcpu[0] instead of NULL?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 11 lines · Jan Beulich
xen/include/xen/sched.h static inline int domain_pause_by_systemcontroller_nosync(struct domain *d) · line 863
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 23.01.19 at 15:59, <[email protected]> wrote:
xen/include/xen/sched.h +7
-0+static inline struct vcpu *domain_vcpu (const struct domain *d, +unsigned int vcpu_id) +{ +unsigned int idx =array_index_nospec (vcpu_id, d->max_vcpus); + +return idx >= d->max_vcpus ?NULL : d->vcpu[idx]; +}For an out of bounds incoming
vcpu_id, isn't it the case that idx then would be zero? In which case you'd return d->vcpu[0] instead of NULL?
Speculatively, yes. array_index_nospec() works by forcing speculative mis-accesses to operate as if it request had been for index 0.
What matters from a data-leaking perspective is whether d->vcpu[idx], when executed speculative, ends up being out-of-bounds or not. i.e. whether it is distinguishable from a path which can architecturally be taken.
~Andrew
P.S. index 0 is actually better than NULL on any hardware lacking SMAP, because you won't potentially use guest-controlled data from 0 during the subsequent speculation.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 23.01.19 at 18:44, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
xen/include/xen/sched.h static inline int domain_pause_by_systemcontroller_nosync(struct domain *d) · line 863
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 23.01.19 at 15:59, <[email protected]> wrote:
xen/include/xen/sched.h +7
-0+static inline struct vcpu *domain_vcpu (const struct domain *d, +unsigned int vcpu_id) +{ +unsigned int idx =array_index_nospec (vcpu_id, d->max_vcpus); + +return idx >= d->max_vcpus ?NULL : d->vcpu[idx]; +}For an out of bounds incoming
vcpu_id, isn't it the case that idx then would be zero? In which case you'd return d->vcpu[0] instead of NULL?
Quoted 7 lines
Speculatively, yes.
array_index_nospec()works by forcing speculative mis-accesses to operate as if it request had been for index 0.What matters from a data-leaking perspective is whether d->vcpu[idx], when executed speculative, ends up being out-of-bounds or not. i.e. whether it is distinguishable from a path which can architecturally be taken.
I'm afraid we're talking of different aspects. I'm not considering the speculation aspect at all, but the mere base functionality. A caller of this is, I think, supposed to check whether it got back NULL in order to know whether the passed in vCPU ID was valid. If non-NULL, it ought to act on that vCPU. But you don't want invalid vCPU IDs getting passed into hypercalls to result in action on vCPU 0, do you? Or are you implying that callers ought to duplicate the bounds checking done here? If so, this would need to be called out very prominently in the comment accompanying the function, to hopefully avoid anyone stepping into this trap.
Quoted 3 lines
P.S. index 0 is actually better than NULL on any hardware lacking SMAP, because you won't potentially use guest-controlled data from 0 during the subsequent speculation.
Is that the case in the way you describe it? I thought one of the base issues with some of last year's speculation issues was that data related #PF get evaluated only at the end of the pipeline, when retiring insns. To me this would imply speculation through NULL is equally happening with SMAP. Furthermore 32-bit PV guests could place a kernel mapping there.
Of course the implication would be that avoiding to hand back NULL has even wider benefit. But then the question is whether handing back NULL here and elsewhere shouldn't be avoided altogether.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 16 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 23.01.19 at 18:44, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
xen/include/xen/sched.h static inline int domain_pause_by_systemcontroller_nosync(struct domain *d) · line 863
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 23.01.19 at 15:59, <[email protected]> wrote:
xen/include/xen/sched.h +7
-0+static inline struct vcpu *domain_vcpu (const struct domain *d, +unsigned int vcpu_id) +{ +unsigned int idx =array_index_nospec (vcpu_id, d->max_vcpus); + +return idx >= d->max_vcpus ?NULL : d->vcpu[idx]; +}For an out of bounds incoming
vcpu_id, isn't it the case that idx then would be zero? In which case you'd return d->vcpu[0] instead of NULL?Speculatively, yes.
array_index_nospec()works by forcing speculative mis-accesses to operate as if it request had been for index 0.
Quoted 6 lines
Quoted 4 lines
What matters from a data-leaking perspective is whether d->vcpu[idx], when executed speculative, ends up being out-of-bounds or not. i.e. whether it is distinguishable from a path which can architecturally be taken.
I'm afraid we're talking of different aspects. I'm not considering the speculation aspect at all, but the mere base functionality.
Oops yes. You're right that is a real non-speculative issue here.
The correct code is:
{
unsigned int idx = array_index_nospec(vcpu_id, d->max_vcpus);
return vcpu_id >= d->max_vcpus ? NULL : d->vcpu[idx]; }
Which will return a real NULL for all non-speculative out-of-bounds requests, and will return d->vcpu[0] during incorrect speculation.
~Andrew
Quoted 4 lines
Quoted 3 lines
P.S. index 0 is actually better than NULL on any hardware lacking SMAP, because you won't potentially use guest-controlled data from 0 during the subsequent speculation.Is that the case in the way you describe it?
The case I had in mind was a guest which goes and mmap()'s something real at 0.
Quoted 4 lines
I thought one of the
base issues with some of last year's speculation issues was that data related #PF get evaluated only at the end of the pipeline, when retiring insns.
That is correct for Meltdown, but you need to get a TLB hit first, so only applies to permission problems on the mapping. (Also, the data needs to be in the L1 cache to leak.)
L1TF covers the other side of things where there isn't a valid mapping, the addresses in question are physical rather than linear. (Also, needs to hit in the L1 cache.)
Quoted 2 lines
To me this would imply speculation through
NULL is equally happening with SMAP.
It is the behind-the-scenes implementation of SMAP which makes it safe on existing processors.
STAC is a TLB flush operation which flushes all user mappings (hence its curious CPL 0 restriction for something which ostensibly just touches EFLAGS.AC), and while AC is set, a pagewalk which results in a user mapping won't result in a TLB fill.
Therefore, when you hit a user mapping, you start with a TLB miss (because user mappings were previously flushed), request a pagewalk (which resolves to a user mapping), and this mapping is deliberately not re-inserted into the TLB, opting instead for "pagewalk resulted in failure", which is how the #PF eventually manifests.
Quoted 1 line
Furthermore 32-bit PV guests could place a kernel mapping there.
Yes, but this is no worse than userspace mmap()'ing a page there in the absence of SMAP.
Quoted 4 lines
Of course the implication would be that avoiding to hand back NULL has even wider benefit. But then the question is whether handing back NULL here and elsewhere shouldn't be avoided altogether.
It is even harder to do without compiler support than the lfence'ing currently under question.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Jan Beulich
Reviewed-byQuoted 29 lines
Quoted 1 line
Quoted 1 line · On 24.01.19 at 13:33, <[email protected]> wrote:
Quoted 16 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 23.01.19 at 18:44, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
xen/include/xen/sched.h static inline int domain_pause_by_systemcontroller_nosync(struct domain *d) · line 863
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 23.01.19 at 15:59, <[email protected]> wrote:
xen/include/xen/sched.h +7
-0+static inline struct vcpu *domain_vcpu (const struct domain *d, +unsigned int vcpu_id) +{ +unsigned int idx =array_index_nospec (vcpu_id, d->max_vcpus); + +return idx >= d->max_vcpus ?NULL : d->vcpu[idx]; +}For an out of bounds incoming
vcpu_id, isn't it the case that idx then would be zero? In which case you'd return d->vcpu[0] instead of NULL?Speculatively, yes.
array_index_nospec()works by forcing speculative mis-accesses to operate as if it request had been for index 0.Quoted 6 lines
Quoted 4 lines
What matters from a data-leaking perspective is whether d->vcpu[idx], when executed speculative, ends up being out-of-bounds or not. i.e. whether it is distinguishable from a path which can architecturally be taken.
I'm afraid we're talking of different aspects. I'm not considering the speculation aspect at all, but the mere base functionality.
Oops yes. You're right that is a real non-speculative issue here.
The correct code is:
Quoted 8 lines
{ unsigned int idx = array_index_nospec(vcpu_id, d->max_vcpus); return vcpu_id >= d->max_vcpus ? NULL : d->vcpu[idx]; }Which will return a real NULL for all non-speculative out-of-bounds requests, and will return d->vcpu[0] during incorrect speculation.
And in this shape it's then
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 6 lines · Andrew Cooper
The progression of multi-vcpu support in Xen (originally a single pointer, then an embedded d->vcpu[] array, then a dynamically allocated array) has resulted in a large quantity of ad-hoc code for looking a vcpu up by id, and a large number of ways that the toolstack can cause Xen to trip over a NULL pointer. Some of this has been addressed in Xen 4.12, and work is ongoing.
Quoted 8 lines
Another property of looking a vcpu up by id is frequently done in unprivileged hypercall context, making it an attractive target for speculative sidechannel attacks.
Introduce a helper to do the lookup correctly, and without speculative interference. For performance reasons, it is useful not to have an
smp_rmb()in this helper on ARM, and luckily this is safe to do, because of the serialisation offered by the global domheap lock.
I think you mean domlist lock here?
Wei.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 11 lines · Wei Liu
Quoted 10 lines · Andrew Cooper
The progression of multi-vcpu support in Xen (originally a single pointer, then an embedded d->vcpu[] array, then a dynamically allocated array) has resulted in a large quantity of ad-hoc code for looking a vcpu up by id, and a large number of ways that the toolstack can cause Xen to trip over a NULL pointer. Some of this has been addressed in Xen 4.12, and work is ongoing.
Another property of looking a vcpu up by id is frequently done in unprivileged hypercall context, making it an attractive target for speculative sidechannel attacks.
Quoted 5 lines
Quoted 4 lines
Introduce a helper to do the lookup correctly, and without speculative interference. For performance reasons, it is useful not to have an
smp_rmb()in this helper on ARM, and luckily this is safe to do, because of the serialisation offered by the global domheap lock.I think you mean domlist lock here?
I do indeed.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Hi,
Quoted 9 lines · Andrew Cooper
+/*
+ * For each allocated vcpu, d->vcpu[X]->vcpu_id == X
+ *
+ * During construction, all vcpus in d->vcpu[] are allocated sequentially, and
+ * in ascending order. Therefore, if d->vcpu[N] exists (e.g. derived from
+ * current), all vcpus with an id less than N also exist.
+ *
+ * SMP considerations: The idle domain is constructed before APs are started.
+ * All other domains have d->vcpu[] allocated andd->max_vcpusset before the
Quoted 8 lines
+ * domain is made visible in the domlist, which is serialised on the global
+ *domlist_update_lock.
+ *
+ * Therefore, all observations ofd->max_vcpusvs d->vcpu[] will be consistent
+ * despite the lack of smp_* barriers, either by being on the same CPU as the
+ * one which issued the writes, or because of barrier properties of the domain
+ * having been inserted into the domlist.
+ */
From an Arm perspective, this makes sense to me.
Cheers,
Signature · 6 lines
Julien Grall _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel