Skip to contentSkip to conversation

x86/hvm: Don't corrupt the HVM context stream when writing the MSR record

PATCH In review Andrew Cooper 4 participants 5 messages Nov 16, 2017 – Nov 21, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/hvm: Don't corrupt the HVM context stream when writing the MSR record

Ever since it was introduced in c/s bd1f0b45ff, hvm_save_cpu_msrs() has had a bug whereby it corrupts the HVM context stream if some, but fewer than the maximum number of MSRs are written.

_hvm_init_entry() creates an hvm_save_descriptor with length for msr_count_max, but in the case that we write fewer than max, h->cur only moves forward by the amount of space used, causing the subsequent hvm_save_descriptor to be written within the bounds of the previous one.

To resolve this, reduce the length reported by the descriptor to match the actual number of bytes used.

A typical failure on the destination side looks like:

(XEN) HVM4 restore: CPU_MSR 0
(XEN) HVM4.0 restore: not enough data left to read 56 MSR bytes (XEN) HVM4 restore: failed to load entry 20/0

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Julien Grall <[email protected]>

This wants backporting to all stable trees, so should also be considered for inclusion into 4.10 at this point.

---
 xen/arch/x86/hvm/hvm.c | 6 ++++++
 1 file changed, 6 insertions(+)
xen/arch/x86/hvm/hvm.c +6 -0
index 0af498a..c5e8467 100644
@@ -1330,6 +1330,7 @@ static int hvm_save_cpu_msrs(struct domain *d, hvm_domain_context_t *h)
 
     for_each_vcpu ( d, v )
     {
+        struct hvm_save_descriptor *d = _p(&h->data[h->cur]);
         struct hvm_msr *ctxt;
         unsigned int i;
 
@@ -1348,8 +1349,13 @@ static int hvm_save_cpu_msrs(struct domain *d, hvm_domain_context_t *h)
             ctxt->msr[i]._rsvd = 0;
 
         if ( ctxt->count )
+        {
+            /* Rewrite length to indicate how much space we actually used. */
+            d->length = HVM_CPU_MSR_SIZE(ctxt->count);
             h->cur += HVM_CPU_MSR_SIZE(ctxt->count);
+        }
         else
+            /* or rewind and remove the descriptor from the stream. */
             h->cur -= sizeof(struct hvm_save_descriptor);
     }
 
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · Andrew Cooper

Ever since it was introduced in c/s bd1f0b45ff, hvm_save_cpu_msrs() has had a bug whereby it corrupts the HVM context stream if some, but fewer than the maximum number of MSRs are written.

_hvm_init_entry() creates an hvm_save_descriptor with length for msr_count_max, but in the case that we write fewer than max, h->cur only moves forward by the amount of space used, causing the subsequent hvm_save_descriptor to be written within the bounds of the previous one.

To resolve this, reduce the length reported by the descriptor to match the actual number of bytes used.

Quoted 7 lines

A typical failure on the destination side looks like:

(XEN) HVM4 restore: CPU_MSR 0
(XEN) HVM4.0 restore: not enough data left to read 56 MSR bytes (XEN) HVM4 restore: failed to load entry 20/0

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v1 1/1
#2 lore raw
In reply to Andrew Cooper
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 16.11.17 at 23:45, <[email protected]> wrote:

Ever since it was introduced in c/s bd1f0b45ff, hvm_save_cpu_msrs() has had a bug whereby it corrupts the HVM context stream if some, but fewer than the maximum number of MSRs are written.

_hvm_init_entry() creates an hvm_save_descriptor with length for msr_count_max, but in the case that we write fewer than max, h->cur only moves forward by the amount of space used, causing the subsequent hvm_save_descriptor to be written within the bounds of the previous one.

To resolve this, reduce the length reported by the descriptor to match the actual number of bytes used.

Quoted 7 lines

A typical failure on the destination side looks like:

(XEN) HVM4 restore: CPU_MSR 0
(XEN) HVM4.0 restore: not enough data left to read 56 MSR bytes (XEN) HVM4 restore: failed to load entry 20/0

Quoted 11 lines
xen/arch/x86/hvm/hvm.c +1 -0
@@ -1330,6 +1330,7 @@ static int hvm_save_cpu_msrs(struct domain *d, hvm_domain_context_t *h)
 
     for_each_vcpu ( d, v )
     {
+        struct hvm_save_descriptor *d = _p(&h->data[h->cur]);
         struct hvm_msr *ctxt;
         unsigned int i;
 

xen/arch/x86/hvm/hvm.c static int hvm_save_cpu_msrs(struct domain *d, hvm_domain_context_t *h) · line 1354

Quoted 6 lines
xen/arch/x86/hvm/hvm.c +3 -0
@@ -1348,8 +1349,13 @@ static int hvm_save_cpu_msrs(struct domain *d, hvm_domain_context_t *h)
             ctxt->msr[i]._rsvd = 0;
 
         if ( ctxt->count )
+        {
+            /* Rewrite length to indicate how much space we actually used. */
+            d->length = HVM_CPU_MSR_SIZE(ctxt->count);

Would of course be nice if we had a function to do this, such that the (sufficiently hidden) cast above also wouldn't be necessary to open code in places like this one.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#3 lore raw
Quoted 35 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 16.11.17 at 23:45, <[email protected]> wrote:

Ever since it was introduced in c/s bd1f0b45ff, hvm_save_cpu_msrs() has had a bug whereby it corrupts the HVM context stream if some, but fewer than the maximum number of MSRs are written.

_hvm_init_entry() creates an hvm_save_descriptor with length for msr_count_max, but in the case that we write fewer than max, h->cur only moves forward by the amount of space used, causing the subsequent hvm_save_descriptor to be written within the bounds of the previous one.

To resolve this, reduce the length reported by the descriptor to match the actual number of bytes used.

Quoted 7 lines

A typical failure on the destination side looks like:

(XEN) HVM4 restore: CPU_MSR 0
(XEN) HVM4.0 restore: not enough data left to read 56 MSR bytes (XEN) HVM4 restore: failed to load entry 20/0

Quoted 13 lines
xen/arch/x86/hvm/hvm.c +1 -0
@@ -1330,6 +1330,7 @@ static int hvm_save_cpu_msrs(struct domain *d, hvm_domain_context_t *h)
 
     for_each_vcpu ( d, v )
     {
+        struct hvm_save_descriptor *d = _p(&h->data[h->cur]);
         struct hvm_msr *ctxt;
         unsigned int i;
 
@@ -1348,8 +1349,13 @@ static int hvm_save_cpu_msrs(struct domain *d, hvm_domain_context_t *h)
             ctxt->msr[i]._rsvd = 0;
 
Quoted 7 lines

xen/arch/x86/hvm/hvm.c static int hvm_save_cpu_msrs(struct domain *d, hvm_domain_context_t *h) · line 1354

Quoted 4 lines
xen/arch/x86/hvm/hvm.c +3 -0
         if ( ctxt->count )
+        {
+            /* Rewrite length to indicate how much space we actually used. */
+            d->length = HVM_CPU_MSR_SIZE(ctxt->count);

Would of course be nice if we had a function to do this, such that the (sufficiently hidden) cast above also wouldn't be necessary to open code in places like this one.

This is the one and only case where we need to rewrite the length.  All records (other than XSAVE) are fixed size, and XSAVE can calculate the exact required size ahead of setting up descriptor in the first place.

(Also, this code isn't long for the world.  It will be changing when the CPUID/MSR policy work is complete, and we can rearrange the migration stream to put data in the order the destination needs to receive it.)

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#4 lore raw
In reply to Andrew Cooper

Hi,

Quoted 18 lines · Andrew Cooper

Ever since it was introduced in c/s bd1f0b45ff, hvm_save_cpu_msrs() has had a bug whereby it corrupts the HVM context stream if some, but fewer than the maximum number of MSRs are written.

_hvm_init_entry() creates an hvm_save_descriptor with length for msr_count_max, but in the case that we write fewer than max, h->cur only moves forward by the amount of space used, causing the subsequent hvm_save_descriptor to be written within the bounds of the previous one.

To resolve this, reduce the length reported by the descriptor to match the actual number of bytes used.

A typical failure on the destination side looks like:

(XEN) HVM4 restore: CPU_MSR 0
(XEN) HVM4.0 restore: not enough data left to read 56 MSR bytes (XEN) HVM4 restore: failed to load entry 20/0

Quoted 8 lines

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Julien Grall <[email protected]>

This wants backporting to all stable trees, so should also be considered for inclusion into 4.10 at this point.

Cheers,

Quoted 21 lines
---
  xen/arch/x86/hvm/hvm.c | 6 ++++++
  1 file changed, 6 insertions(+)
xen/arch/x86/hvm/hvm.c +1 -0
index 0af498a..c5e8467 100644
@@ -1330,6 +1330,7 @@ static int hvm_save_cpu_msrs(struct domain *d, hvm_domain_context_t *h)
 
      for_each_vcpu ( d, v )
      {
+        struct hvm_save_descriptor *d = _p(&h->data[h->cur]);
          struct hvm_msr *ctxt;
          unsigned int i;
 
@@ -1348,8 +1349,13 @@ static int hvm_save_cpu_msrs(struct domain *d, hvm_domain_context_t *h)
              ctxt->msr[i]._rsvd = 0;
 
          if ( ctxt->count )

xen/arch/x86/hvm/hvm.c static int hvm_save_cpu_msrs(struct domain *d, hvm_domain_context_t *h) · line 1361

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +5 -0
+        {
+            /* Rewrite length to indicate how much space we actually used. */
+            d->length = HVM_CPU_MSR_SIZE(ctxt->count);
              h->cur += HVM_CPU_MSR_SIZE(ctxt->count);
+        }
          else
+            /* or rewind and remove the descriptor from the stream. */
              h->cur -= sizeof(struct hvm_save_descriptor);
      }
 

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel