Skip to contentSkip to conversation

nested vmx: fix instruction decode segment limit check

PATCH Superseded Jan Beulich 2 participants 3 messages Apr 24, 2012 – Apr 25, 2012

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nested vmx: fix instruction decode segment limit check

- no limit check in 64-bit mode (is not special in any way)
- limit check is needed in compatibility mode
- canonical address check should instead be performed in 64-bit mode
- the last accessed byte must be within limits, not the first byte past the accessed range

xen/arch/x86/hvm/vmx/vvmx.c +10 -3
@@ -319,7 +319,7 @@ static int decode_vmx_inst(struct cpu_us
 {
     struct vcpu *v = current;
     union vmx_inst_info info;
-    struct segment_register seg;
+    struct segment_register seg, cs;
     unsigned long base, index, seg_base, disp, offset;
     int scale, size;
 
@@ -342,6 +342,11 @@ static int decode_vmx_inst(struct cpu_us
         hvm_get_segment_register(v, sreg_to_index[info.fields.segment], &seg);
         seg_base = seg.base;
 
+        if ( hvm_long_mode_enabled(v) )
+            hvm_get_segment_register(v, x86_seg_cs, &cs);
+        else
+            memset(&cs, 0, sizeof(cs));
+
         base = info.fields.base_reg_invalid ? 0 :
             reg_read(regs, info.fields.base_reg);
 
@@ -355,8 +360,10 @@ static int decode_vmx_inst(struct cpu_us
         size = 1 << (info.fields.addr_size + 1);
 
         offset = base + index * scale + disp;
-        if ( (offset > seg.limit || offset + size > seg.limit) &&
-            (!hvm_long_mode_enabled(v) || info.fields.segment == VMX_SREG_GS) )
+        if ( cs.attr.fields.l ?
+             (!is_canonical_address(offset) ||
+              !is_canonical_address(offset + size - 1)) :
+             (offset + size - 1 < offset || offset + size - 1 > seg.limit) )
             goto gp_fault;
 
         if ( poperandS != NULL &&

nested vmx: fix instruction decode segment limit check

- no limit check in 64-bit mode (is not special in any way)
- limit check is needed in compatibility mode
- canonical address check should instead be performed in 64-bit mode
- the last accessed byte must be within limits, not the first byte past the accessed range

xen/arch/x86/hvm/vmx/vvmx.c +10 -3
@@ -319,7 +319,7 @@ static int decode_vmx_inst(struct cpu_us
 {
     struct vcpu *v = current;
     union vmx_inst_info info;
-    struct segment_register seg;
+    struct segment_register seg, cs;
     unsigned long base, index, seg_base, disp, offset;
     int scale, size;
 
@@ -342,6 +342,11 @@ static int decode_vmx_inst(struct cpu_us
         hvm_get_segment_register(v, sreg_to_index[info.fields.segment], &seg);
         seg_base = seg.base;
 
+        if ( hvm_long_mode_enabled(v) )
+            hvm_get_segment_register(v, x86_seg_cs, &cs);
+        else
+            memset(&cs, 0, sizeof(cs));
+
         base = info.fields.base_reg_invalid ? 0 :
             reg_read(regs, info.fields.base_reg);
 
@@ -355,8 +360,10 @@ static int decode_vmx_inst(struct cpu_us
         size = 1 << (info.fields.addr_size + 1);
 
         offset = base + index * scale + disp;
-        if ( (offset > seg.limit || offset + size > seg.limit) &&
-            (!hvm_long_mode_enabled(v) || info.fields.segment == VMX_SREG_GS) )
+        if ( cs.attr.fields.l ?
+             (!is_canonical_address(offset) ||
+              !is_canonical_address(offset + size - 1)) :
+             (offset + size - 1 < offset || offset + size - 1 > seg.limit) )
             goto gp_fault;
 
         if ( poperandS != NULL &&

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

#1 lore raw

Hi,

Quoted 20 lines · Jan Beulich

- no limit check in 64-bit mode (is not special in any way)
- limit check is needed in compatibility mode
- canonical address check should instead be performed in 64-bit mode
- the last accessed byte must be within limits, not the first byte past the accessed range

xen/arch/x86/hvm/vmx/vvmx.c +1 -1
@@ -319,7 +319,7 @@ static int decode_vmx_inst(struct cpu_us
 {
     struct vcpu *v = current;
     union vmx_inst_info info;
-    struct segment_register seg;
+    struct segment_register seg, cs;
     unsigned long base, index, seg_base, disp, offset;
     int scale, size;
 

xen/arch/x86/hvm/vmx/vvmx.c static int decode_vmx_inst(struct cpu_us · line 349

Quoted 8 lines
xen/arch/x86/hvm/vmx/vvmx.c +5 -0
@@ -342,6 +342,11 @@ static int decode_vmx_inst(struct cpu_us
         hvm_get_segment_register(v, sreg_to_index[info.fields.segment], &seg);
         seg_base = seg.base;
 
+        if ( hvm_long_mode_enabled(v) )
+            hvm_get_segment_register(v, x86_seg_cs, &cs);
+        else
+            memset(&cs, 0, sizeof(cs));
+

I found this a bit confusing - maybe you could extract the attr.fields.l bit into a bool here instead of zeroing the struct and extracting it later?

Cheers,

Tim.

#2 lore raw
Quoted 5 lines
Quoted 5 lines
Quoted 1 line · Tim Deegan
xen/arch/x86/hvm/vmx/vvmx.c +0 -0
@@ -342,6 +342,11 @@ static int decode_vmx_inst(struct cpu_us
         hvm_get_segment_register(v, sreg_to_index[info.fields.segment], &seg);
         seg_base = seg.base;
 
Quoted 8 lines

xen/arch/x86/hvm/vmx/vvmx.c static int decode_vmx_inst(struct cpu_us · line 349

Quoted 5 lines
xen/arch/x86/hvm/vmx/vvmx.c +5 -0
+        if ( hvm_long_mode_enabled(v) )
+            hvm_get_segment_register(v, x86_seg_cs, &cs);
+        else
+            memset(&cs, 0, sizeof(cs));
+

I found this a bit confusing - maybe you could extract the attr.fields.l bit into a bool here instead of zeroing the struct and extracting it later?

Okay, let me re-do that, and fold in some other adjustments I thought of making after having sent out the patch.

Jan