Skip to contentSkip to conversation

x86/HVM: XSA-192 follow-ups

PATCH In review Jan Beulich 3 participants 12 messages Nov 22, 2016 – Nov 23, 2016

lore
Share & tools
lore
#1 lore raw

PATCH 1/3 x86/HVM: limit writes to incoming TSS during task switch

The only field modified (and even that conditionally) is the back link. Write only that field, and only when it actually has been written to.

Take the opportunity and also ditch the pointless initializer from the "tss" local variable.

xen/arch/x86/hvm/hvm.c +13 -13
@@ -2890,7 +2890,7 @@ void hvm_task_switch(
         u32 cr3, eip, eflags, eax, ecx, edx, ebx, esp, ebp, esi, edi;
         u16 es, _3, cs, _4, ss, _5, ds, _6, fs, _7, gs, _8, ldt, _9;
         u16 trace, iomap;
-    } tss = { 0 };
+    } tss;
 
     hvm_get_segment_register(v, x86_seg_gdtr, &gdt);
     hvm_get_segment_register(v, x86_seg_tr, &prev_tr);
@@ -3010,12 +3010,6 @@ void hvm_task_switch(
     regs->esi    = tss.esi;
     regs->edi    = tss.edi;
 
-    if ( (taskswitch_reason == TSW_call_or_int) )
-    {
-        regs->eflags |= X86_EFLAGS_NT;
-        tss.back_link = prev_tr.sel;
-    }
-
     exn_raised = 0;
     if ( hvm_load_segment_selector(x86_seg_es, tss.es, tss.eflags) ||
          hvm_load_segment_selector(x86_seg_cs, tss.cs, tss.eflags) ||
@@ -3025,12 +3019,18 @@ void hvm_task_switch(
          hvm_load_segment_selector(x86_seg_gs, tss.gs, tss.eflags) )
         exn_raised = 1;
 
-    rc = hvm_copy_to_guest_virt(
-        tr.base, &tss, sizeof(tss), PFEC_page_present);
-    if ( rc == HVMCOPY_bad_gva_to_gfn )
-        exn_raised = 1;
-    else if ( rc != HVMCOPY_okay )
-        goto out;
+    if ( taskswitch_reason == TSW_call_or_int )
+    {
+        regs->eflags |= X86_EFLAGS_NT;
+        tss.back_link = prev_tr.sel;
+
+        rc = hvm_copy_to_guest_virt(tr.base + offsetof(typeof(tss), back_link),
+                                    &tss.back_link, sizeof(tss.back_link), 0);
+        if ( rc == HVMCOPY_bad_gva_to_gfn )
+            exn_raised = 1;
+        else if ( rc != HVMCOPY_okay )
+            goto out;
+    }
 
     if ( (tss.trace & 1) && !exn_raised )
         hvm_inject_hw_exception(TRAP_debug, HVM_DELIVER_NO_ERROR_CODE);

x86/HVM: limit writes to incoming TSS during task switch

The only field modified (and even that conditionally) is the back link. Write only that field, and only when it actually has been written to.

Take the opportunity and also ditch the pointless initializer from the "tss" local variable.

xen/arch/x86/hvm/hvm.c +13 -13
@@ -2890,7 +2890,7 @@ void hvm_task_switch(
         u32 cr3, eip, eflags, eax, ecx, edx, ebx, esp, ebp, esi, edi;
         u16 es, _3, cs, _4, ss, _5, ds, _6, fs, _7, gs, _8, ldt, _9;
         u16 trace, iomap;
-    } tss = { 0 };
+    } tss;
 
     hvm_get_segment_register(v, x86_seg_gdtr, &gdt);
     hvm_get_segment_register(v, x86_seg_tr, &prev_tr);
@@ -3010,12 +3010,6 @@ void hvm_task_switch(
     regs->esi    = tss.esi;
     regs->edi    = tss.edi;
 
-    if ( (taskswitch_reason == TSW_call_or_int) )
-    {
-        regs->eflags |= X86_EFLAGS_NT;
-        tss.back_link = prev_tr.sel;
-    }
-
     exn_raised = 0;
     if ( hvm_load_segment_selector(x86_seg_es, tss.es, tss.eflags) ||
          hvm_load_segment_selector(x86_seg_cs, tss.cs, tss.eflags) ||
@@ -3025,12 +3019,18 @@ void hvm_task_switch(
          hvm_load_segment_selector(x86_seg_gs, tss.gs, tss.eflags) )
         exn_raised = 1;
 
-    rc = hvm_copy_to_guest_virt(
-        tr.base, &tss, sizeof(tss), PFEC_page_present);
-    if ( rc == HVMCOPY_bad_gva_to_gfn )
-        exn_raised = 1;
-    else if ( rc != HVMCOPY_okay )
-        goto out;
+    if ( taskswitch_reason == TSW_call_or_int )
+    {
+        regs->eflags |= X86_EFLAGS_NT;
+        tss.back_link = prev_tr.sel;
+
+        rc = hvm_copy_to_guest_virt(tr.base + offsetof(typeof(tss), back_link),
+                                    &tss.back_link, sizeof(tss.back_link), 0);
+        if ( rc == HVMCOPY_bad_gva_to_gfn )
+            exn_raised = 1;
+        else if ( rc != HVMCOPY_okay )
+            goto out;
+    }
 
     if ( (tss.trace & 1) && !exn_raised )
         hvm_inject_hw_exception(TRAP_debug, HVM_DELIVER_NO_ERROR_CODE);

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

#2 lore raw

Re: [PATCH 1/3] x86/HVM: limit writes to incoming TSS during task switch

Quoted 5 lines · Jan Beulich

The only field modified (and even that conditionally) is the back link. Write only that field, and only when it actually has been written to.

Take the opportunity and also ditch the pointless initializer from the "tss" local variable.

It would help to point out that tss is unconditionally filled completely from guest memory.

Quoted 1 line

As for the mechanical adjustments here, Reviewed-by: Andrew Cooper <[email protected]>

However, is the position of the backlink write actually correct? I'd have thought that all access to the old tss happen before switching cr3.

I can't find a useful description of the order of events in a task switch in either manual.

~Andrew

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

#3 lore raw

Re: [PATCH 1/3] x86/HVM: limit writes to incoming TSS during task switch

Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 22.11.16 at 17:32, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich

The only field modified (and even that conditionally) is the back link. Write only that field, and only when it actually has been written to.

Take the opportunity and also ditch the pointless initializer from the "tss" local variable.

It would help to point out that tss is unconditionally filled completely from guest memory.

Quoted 7 lines
Quoted 1 line

As for the mechanical adjustments here, Reviewed-by: Andrew Cooper <[email protected]>

However, is the position of the backlink write actually correct? I'd have thought that all access to the old tss happen before switching cr3.

But the backlink gets written into the incoming TSS. And I think it is being assumed anyway that both TSSes (just like the GDT) are visible through either CR3, the more that the incoming TSS necessarily is being read in the old address space context.

Jan

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

#4 lore raw

Re: [PATCH 1/3] x86/HVM: limit writes to incoming TSS during task switch

Quoted 10 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 22.11.16 at 17:32, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich

The only field modified (and even that conditionally) is the back link. Write only that field, and only when it actually has been written to.

Take the opportunity and also ditch the pointless initializer from the "tss" local variable.

It would help to point out that tss is unconditionally filled completely from guest memory.

Quoted 7 lines
Quoted 6 lines
Quoted 1 line

As for the mechanical adjustments here, Reviewed-by: Andrew Cooper <[email protected]>

However, is the position of the backlink write actually correct? I'd have thought that all access to the old tss happen before switching cr3.

But the backlink gets written into the incoming TSS.

Ah - of course it does. I was getting confused by the repeated use of the tss structure. Sorry for the noise.

~Andrew

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

#5 lore raw

PATCH 2/3 x86/HVM: limit writes to outgoing TSS during task switch

In reply to Jan Beulich

The only fields modified are EIP, EFLAGS, GPRs, and segment selectors. CR3 in particular is not supposed to be updated.

xen/arch/x86/hvm/hvm.c +4 -3
@@ -2952,7 +2952,6 @@ void hvm_task_switch(
     if ( taskswitch_reason == TSW_iret )
         eflags &= ~X86_EFLAGS_NT;
 
-    tss.cr3    = v->arch.hvm_vcpu.guest_cr[3];
     tss.eip    = regs->eip;
     tss.eflags = eflags;
     tss.eax    = regs->eax;
@@ -2979,8 +2978,10 @@ void hvm_task_switch(
     hvm_get_segment_register(v, x86_seg_ldtr, &segr);
     tss.ldt = segr.sel;
 
-    rc = hvm_copy_to_guest_virt(
-        prev_tr.base, &tss, sizeof(tss), PFEC_page_present);
+    rc = hvm_copy_to_guest_virt(prev_tr.base + offsetof(typeof(tss), eip),
+                                &tss.eip,
+                                (void *)&tss.trace - (void *)&tss.eip,
+                                PFEC_page_present);
     if ( rc != HVMCOPY_okay )
         goto out;
 

x86/HVM: limit writes to outgoing TSS during task switch

The only fields modified are EIP, EFLAGS, GPRs, and segment selectors. CR3 in particular is not supposed to be updated.

xen/arch/x86/hvm/hvm.c +4 -3
@@ -2952,7 +2952,6 @@ void hvm_task_switch(
     if ( taskswitch_reason == TSW_iret )
         eflags &= ~X86_EFLAGS_NT;
 
-    tss.cr3    = v->arch.hvm_vcpu.guest_cr[3];
     tss.eip    = regs->eip;
     tss.eflags = eflags;
     tss.eax    = regs->eax;
@@ -2979,8 +2978,10 @@ void hvm_task_switch(
     hvm_get_segment_register(v, x86_seg_ldtr, &segr);
     tss.ldt = segr.sel;
 
-    rc = hvm_copy_to_guest_virt(
-        prev_tr.base, &tss, sizeof(tss), PFEC_page_present);
+    rc = hvm_copy_to_guest_virt(prev_tr.base + offsetof(typeof(tss), eip),
+                                &tss.eip,
+                                (void *)&tss.trace - (void *)&tss.eip,
+                                PFEC_page_present);
     if ( rc != HVMCOPY_okay )
         goto out;
 

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

#6 lore raw

Re: [PATCH 2/3] x86/HVM: limit writes to outgoing TSS during task switch

Quoted 16 lines · Jan Beulich

The only fields modified are EIP, EFLAGS, GPRs, and segment selectors. CR3 in particular is not supposed to be updated.

xen/arch/x86/hvm/hvm.c +0 -1
@@ -2952,7 +2952,6 @@ void hvm_task_switch(
     if ( taskswitch_reason == TSW_iret )
         eflags &= ~X86_EFLAGS_NT;
 
-    tss.cr3    = v->arch.hvm_vcpu.guest_cr[3];
     tss.eip    = regs->eip;
     tss.eflags = eflags;
     tss.eax    = regs->eax;

xen/arch/x86/hvm/hvm.c void hvm_task_switch( · line 2983

Quoted 8 lines
xen/arch/x86/hvm/hvm.c +3 -2
@@ -2979,8 +2978,10 @@ void hvm_task_switch(
     hvm_get_segment_register(v, x86_seg_ldtr, &segr);
     tss.ldt = segr.sel;
 
-    rc = hvm_copy_to_guest_virt(
-        prev_tr.base, &tss, sizeof(tss), PFEC_page_present);
+    rc = hvm_copy_to_guest_virt(prev_tr.base + offsetof(typeof(tss), eip),
+                                &tss.eip,
+                                (void *)&tss.trace - (void *)&tss.eip,

How about offsetof(typeof(tss), trace) - offsetof(typeof(tss), eip)?

It avoids some casts and void pointer arithmetic.

Either way, Reviewed-by: Andrew Cooper <[email protected]>

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

#7 lore raw

Re: [PATCH 2/3] x86/HVM: limit writes to outgoing TSS during task switch

Quoted 29 lines
Quoted 1 line
Quoted 1 line · On 22.11.16 at 17:46, <[email protected]> wrote:
Quoted 16 lines · Jan Beulich

The only fields modified are EIP, EFLAGS, GPRs, and segment selectors. CR3 in particular is not supposed to be updated.

xen/arch/x86/hvm/hvm.c +0 -1
@@ -2952,7 +2952,6 @@ void hvm_task_switch(
     if ( taskswitch_reason == TSW_iret )
         eflags &= ~X86_EFLAGS_NT;
 
-    tss.cr3    = v->arch.hvm_vcpu.guest_cr[3];
     tss.eip    = regs->eip;
     tss.eflags = eflags;
     tss.eax    = regs->eax;

xen/arch/x86/hvm/hvm.c void hvm_task_switch( · line 2983

Quoted 8 lines
xen/arch/x86/hvm/hvm.c +3 -2
@@ -2979,8 +2978,10 @@ void hvm_task_switch(
     hvm_get_segment_register(v, x86_seg_ldtr, &segr);
     tss.ldt = segr.sel;
 
-    rc = hvm_copy_to_guest_virt(
-        prev_tr.base, &tss, sizeof(tss), PFEC_page_present);
+    rc = hvm_copy_to_guest_virt(prev_tr.base + offsetof(typeof(tss), eip),
+                                &tss.eip,
+                                (void *)&tss.trace - (void *)&tss.eip,

How about offsetof(typeof(tss), trace) - offsetof(typeof(tss), eip)?

Quoted 1 line

It avoids some casts and void pointer arithmetic.

Oh, yes, that's better.

Quoted 1 line

Either way, Reviewed-by: Andrew Cooper <[email protected]>

Thanks, Jan

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

#8 lore raw

PATCH 3/3 x86/HVM: correct error code writing during task switch

In reply to Jan Beulich

Whether to write 32 or just 16 bits depends on the D bit of the target CS. The width of the stack pointer to use depends on the B bit of the target SS.

Also avoid using the no-fault copying routine.

Finally avoid using yet another struct segment_register variable here.

xen/arch/x86/hvm/hvm.c +22 -10
@@ -3033,9 +3033,6 @@ void hvm_task_switch(
             goto out;
     }
 
-    if ( (tss.trace & 1) && !exn_raised )
-        hvm_inject_hw_exception(TRAP_debug, HVM_DELIVER_NO_ERROR_CODE);
-
     tr.attr.fields.type = 0xb; /* busy 32-bit tss */
     hvm_set_segment_register(v, x86_seg_tr, &tr);
 
@@ -3051,17 +3048,32 @@ void hvm_task_switch(
 
     if ( errcode >= 0 )
     {
-        struct segment_register reg;
         unsigned long linear_addr;
-        regs->esp -= 4;
-        hvm_get_segment_register(current, x86_seg_ss, &reg);
-        /* Todo: do not ignore access faults here. */
-        if ( hvm_virtual_to_linear_addr(x86_seg_ss, &reg, regs->esp,
-                                        4, hvm_access_write, 32,
+        unsigned int opsz, sp;
+
+        hvm_get_segment_register(current, x86_seg_cs, &segr);
+        opsz = segr.attr.fields.db ? 4 : 2;
+        hvm_get_segment_register(current, x86_seg_ss, &segr);
+        if ( segr.attr.fields.db )
+            sp = regs->_esp -= opsz;
+        else
+            sp = *(uint16_t *)&regs->esp -= opsz;
+        if ( hvm_virtual_to_linear_addr(x86_seg_ss, &segr, sp, opsz,
+                                        hvm_access_write,
+                                        16 << segr.attr.fields.db,
                                         &linear_addr) )
-            hvm_copy_to_guest_virt_nofault(linear_addr, &errcode, 4, 0);
+        {
+            rc = hvm_copy_to_guest_virt(linear_addr, &errcode, opsz, 0);
+            if ( rc == HVMCOPY_bad_gva_to_gfn )
+                exn_raised = 1;
+            else if ( rc != HVMCOPY_okay )
+                goto out;
+        }
     }
 
+    if ( (tss.trace & 1) && !exn_raised )
+        hvm_inject_hw_exception(TRAP_debug, HVM_DELIVER_NO_ERROR_CODE);
+
  out:
     hvm_unmap_entry(optss_desc);
     hvm_unmap_entry(nptss_desc);

x86/HVM: correct error code writing during task switch

Whether to write 32 or just 16 bits depends on the D bit of the target CS. The width of the stack pointer to use depends on the B bit of the target SS.

Also avoid using the no-fault copying routine.

Finally avoid using yet another struct segment_register variable here.

xen/arch/x86/hvm/hvm.c +22 -10
@@ -3033,9 +3033,6 @@ void hvm_task_switch(
             goto out;
     }
 
-    if ( (tss.trace & 1) && !exn_raised )
-        hvm_inject_hw_exception(TRAP_debug, HVM_DELIVER_NO_ERROR_CODE);
-
     tr.attr.fields.type = 0xb; /* busy 32-bit tss */
     hvm_set_segment_register(v, x86_seg_tr, &tr);
 
@@ -3051,17 +3048,32 @@ void hvm_task_switch(
 
     if ( errcode >= 0 )
     {
-        struct segment_register reg;
         unsigned long linear_addr;
-        regs->esp -= 4;
-        hvm_get_segment_register(current, x86_seg_ss, &reg);
-        /* Todo: do not ignore access faults here. */
-        if ( hvm_virtual_to_linear_addr(x86_seg_ss, &reg, regs->esp,
-                                        4, hvm_access_write, 32,
+        unsigned int opsz, sp;
+
+        hvm_get_segment_register(current, x86_seg_cs, &segr);
+        opsz = segr.attr.fields.db ? 4 : 2;
+        hvm_get_segment_register(current, x86_seg_ss, &segr);
+        if ( segr.attr.fields.db )
+            sp = regs->_esp -= opsz;
+        else
+            sp = *(uint16_t *)&regs->esp -= opsz;
+        if ( hvm_virtual_to_linear_addr(x86_seg_ss, &segr, sp, opsz,
+                                        hvm_access_write,
+                                        16 << segr.attr.fields.db,
                                         &linear_addr) )
-            hvm_copy_to_guest_virt_nofault(linear_addr, &errcode, 4, 0);
+        {
+            rc = hvm_copy_to_guest_virt(linear_addr, &errcode, opsz, 0);
+            if ( rc == HVMCOPY_bad_gva_to_gfn )
+                exn_raised = 1;
+            else if ( rc != HVMCOPY_okay )
+                goto out;
+        }
     }
 
+    if ( (tss.trace & 1) && !exn_raised )
+        hvm_inject_hw_exception(TRAP_debug, HVM_DELIVER_NO_ERROR_CODE);
+
  out:
     hvm_unmap_entry(optss_desc);
     hvm_unmap_entry(nptss_desc);

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

#9 lore raw

Re: [PATCH 3/3] x86/HVM: correct error code writing during task switch

Quoted 26 lines · Jan Beulich

Whether to write 32 or just 16 bits depends on the D bit of the target CS. The width of the stack pointer to use depends on the B bit of the target SS.

Also avoid using the no-fault copying routine.

Finally avoid using yet another struct segment_register variable here.

xen/arch/x86/hvm/hvm.c +0 -3
@@ -3033,9 +3033,6 @@ void hvm_task_switch(
             goto out;
     }
 
-    if ( (tss.trace & 1) && !exn_raised )
-        hvm_inject_hw_exception(TRAP_debug, HVM_DELIVER_NO_ERROR_CODE);
-
     tr.attr.fields.type = 0xb; /* busy 32-bit tss */
     hvm_set_segment_register(v, x86_seg_tr, &tr);
 
@@ -3051,17 +3048,32 @@ void hvm_task_switch(
 
     if ( errcode >= 0 )
     {

xen/arch/x86/hvm/hvm.c void hvm_task_switch( · line 3054

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +3 -6
-        struct segment_register reg;
         unsigned long linear_addr;
-        regs->esp -= 4;
-        hvm_get_segment_register(current, x86_seg_ss, &reg);
-        /* Todo: do not ignore access faults here. */
-        if ( hvm_virtual_to_linear_addr(x86_seg_ss, &reg, regs->esp,
-                                        4, hvm_access_write, 32,
+        unsigned int opsz, sp;
+
+        hvm_get_segment_register(current, x86_seg_cs, &segr);

You already have current latched in v at this point.

Otherwise, Reviewed-by: Andrew Cooper <[email protected]>

Quoted 19 lines
xen/arch/x86/hvm/hvm.c +14 -1
+        opsz = segr.attr.fields.db ? 4 : 2;
+        hvm_get_segment_register(current, x86_seg_ss, &segr);
+        if ( segr.attr.fields.db )
+            sp = regs->_esp -= opsz;
+        else
+            sp = *(uint16_t *)&regs->esp -= opsz;
+        if ( hvm_virtual_to_linear_addr(x86_seg_ss, &segr, sp, opsz,
+                                        hvm_access_write,
+                                        16 << segr.attr.fields.db,
                                         &linear_addr) )
-            hvm_copy_to_guest_virt_nofault(linear_addr, &errcode, 4, 0);
+        {
+            rc = hvm_copy_to_guest_virt(linear_addr, &errcode, opsz, 0);
+            if ( rc == HVMCOPY_bad_gva_to_gfn )
+                exn_raised = 1;
+            else if ( rc != HVMCOPY_okay )

xen/arch/x86/hvm/hvm.c void hvm_task_switch( · line 3079

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +5 -0
+                goto out;
+        }
     }
 
+    if ( (tss.trace & 1) && !exn_raised )
+        hvm_inject_hw_exception(TRAP_debug, HVM_DELIVER_NO_ERROR_CODE);
+
  out:
     hvm_unmap_entry(optss_desc);
     hvm_unmap_entry(nptss_desc);

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

#10 lore raw

Re: [PATCH 3/3] x86/HVM: correct error code writing during task switch

Quoted 26 lines
Quoted 1 line
Quoted 1 line · On 22.11.16 at 17:58, <[email protected]> wrote:
Quoted 24 lines · Jan Beulich

Whether to write 32 or just 16 bits depends on the D bit of the target CS. The width of the stack pointer to use depends on the B bit of the target SS.

Also avoid using the no-fault copying routine.

Finally avoid using yet another struct segment_register variable here.

xen/arch/x86/hvm/hvm.c +0 -3
@@ -3033,9 +3033,6 @@ void hvm_task_switch(
             goto out;
     }
 
-    if ( (tss.trace & 1) && !exn_raised )
-        hvm_inject_hw_exception(TRAP_debug, HVM_DELIVER_NO_ERROR_CODE);
-
     tr.attr.fields.type = 0xb; /* busy 32-bit tss */
     hvm_set_segment_register(v, x86_seg_tr, &tr);
 
@@ -3051,17 +3048,32 @@ void hvm_task_switch(
 
Quoted 14 lines

xen/arch/x86/hvm/hvm.c void hvm_task_switch( · line 3054

Quoted 12 lines
xen/arch/x86/hvm/hvm.c +3 -6
     if ( errcode >= 0 )
     {
-        struct segment_register reg;
         unsigned long linear_addr;
-        regs->esp -= 4;
-        hvm_get_segment_register(current, x86_seg_ss, &reg);
-        /* Todo: do not ignore access faults here. */
-        if ( hvm_virtual_to_linear_addr(x86_seg_ss, &reg, regs->esp,
-                                        4, hvm_access_write, 32,
+        unsigned int opsz, sp;
+
+        hvm_get_segment_register(current, x86_seg_cs, &segr);

You already have current latched in v at this point.

Oh, right - I've blindly modified the old function invocation.

Quoted 1 line

Otherwise, Reviewed-by: Andrew Cooper <[email protected]>

Thanks, Jan

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