Skip to contentSkip to conversation

x86/PV: descriptor read sanity checking

PATCH In review Jan Beulich 2 participants 6 messages Nov 24 – Nov 25

lore
Share & tools
lore
#0 lore raw

cover x86/PV: descriptor read sanity checking

1: correct descriptor read sanity check
2: modernize descriptor read sanity check

Jan

#1 lore raw

PATCH 1/2 x86/PV: correct descriptor read sanity check

Checking what LAR returns against what we read from the descriptor table requires that we check unaltered values. The clearing of L therefore needs
limiting: Do that just in *ar, and use the then unaltered desc.b in the BUG_ON() instead.

Fixes 8d6e195a2def ("Adjust emulation code to deal with compatibility mode guests")
xen/arch/x86/pv/emulate.c +6 -4
@@ -37,11 +37,12 @@ int pv_emul_read_descriptor(unsigned int
             return 0;
     }
 
-    if ( !insn_fetch )
-        desc.b &= ~_SEGMENT_L;
 
     *ar = desc.b & 0x00f0ff00;
-    if ( !(desc.b & _SEGMENT_L) )
+    if ( !insn_fetch )
+        *ar &= ~_SEGMENT_L;
+
+    if ( !(*ar & _SEGMENT_L) )
     {
         *base = ((desc.a >> 16) + ((desc.b & 0xff) << 16) +
                  (desc.b & 0xff000000U));
@@ -57,7 +58,8 @@ int pv_emul_read_descriptor(unsigned int
             asm volatile (
                 "larl %2,%0 ; setz %1"
                 : "=r" (a), "=qm" (valid) : "rm" (sel));
-            BUG_ON(valid && ((a & 0x00f0ff00) != *ar));
+            BUG_ON(valid && ((a ^ desc.b) & 0x00f0ff00));
+
             asm volatile (
                 "lsll %2,%0 ; setz %1"
                 : "=r" (l), "=qm" (valid) : "rm" (sel));
#2 lore raw

Re: [PATCH 1/2] x86/PV: correct descriptor read sanity check

Quoted 29 lines · Jan Beulich

Checking what LAR returns against what we read from the descriptor table requires that we check unaltered values. The clearing of L therefore needs
limiting: Do that just in *ar, and use the then unaltered desc.b in the BUG_ON() instead.

Fixes 8d6e195a2def ("Adjust emulation code to deal with compatibility mode guests")
xen/arch/x86/pv/emulate.c +4 -3
@@ -37,11 +37,12 @@ int pv_emul_read_descriptor(unsigned int
             return 0;
     }
 
-    if ( !insn_fetch )
-        desc.b &= ~_SEGMENT_L;
 
     *ar = desc.b & 0x00f0ff00;
-    if ( !(desc.b & _SEGMENT_L) )
+    if ( !insn_fetch )
+        *ar &= ~_SEGMENT_L;
+
+    if ( !(*ar & _SEGMENT_L) )
     {
         *base = ((desc.a >> 16) + ((desc.b & 0xff) << 16) +
                  (desc.b & 0xff000000U));

xen/arch/x86/pv/emulate.c int pv_emul_read_descriptor(unsigned int · line 65

Quoted 9 lines
xen/arch/x86/pv/emulate.c +2 -1
@@ -57,7 +58,8 @@ int pv_emul_read_descriptor(unsigned int
             asm volatile (
                 "larl %2,%0 ; setz %1"
                 : "=r" (a), "=qm" (valid) : "rm" (sel));
-            BUG_ON(valid && ((a & 0x00f0ff00) != *ar));
+            BUG_ON(valid && ((a ^ desc.b) & 0x00f0ff00));
+
             asm volatile (
                 "lsll %2,%0 ; setz %1"
                 : "=r" (l), "=qm" (valid) : "rm" (sel));

This is a TOCTOU race, which reads the GDT/LDT 3 separate times.

Nothing prevents another CPU issuing do_update_descriptor() against this descriptor, and hitting the BUG_ON() despite the care taken in this patch.  Nor is it legitimate for there to be such an interlock.

I suggest we just delete the CONFIG_DEBUG section.  Because the segment registers aren't architecturally available for PV guests, this check simply cannot be made safe.

~Andrew

#3 lore raw

Re: [PATCH 1/2] x86/PV: correct descriptor read sanity check

Quoted 33 lines · Andrew Cooper
Quoted 32 lines · Jan Beulich

Checking what LAR returns against what we read from the descriptor table requires that we check unaltered values. The clearing of L therefore needs
limiting: Do that just in *ar, and use the then unaltered desc.b in the BUG_ON() instead.

Fixes 8d6e195a2def ("Adjust emulation code to deal with compatibility mode guests")
xen/arch/x86/pv/emulate.c +4 -4
@@ -37,11 +37,12 @@ int pv_emul_read_descriptor(unsigned int
             return 0;
     }
 
-    if ( !insn_fetch )
-        desc.b &= ~_SEGMENT_L;
 
     *ar = desc.b & 0x00f0ff00;
-    if ( !(desc.b & _SEGMENT_L) )
+    if ( !insn_fetch )
+        *ar &= ~_SEGMENT_L;
+
+    if ( !(*ar & _SEGMENT_L) )
     {
         *base = ((desc.a >> 16) + ((desc.b & 0xff) << 16) +
                  (desc.b & 0xff000000U));
@@ -57,7 +58,8 @@ int pv_emul_read_descriptor(unsigned int
             asm volatile (
                 "larl %2,%0 ; setz %1"
                 : "=r" (a), "=qm" (valid) : "rm" (sel));
-            BUG_ON(valid && ((a & 0x00f0ff00) != *ar));
Quoted 8 lines

xen/arch/x86/pv/emulate.c int pv_emul_read_descriptor(unsigned int · line 65

Quoted 6 lines
xen/arch/x86/pv/emulate.c +2 -0
+            BUG_ON(valid && ((a ^ desc.b) & 0x00f0ff00));
+
             asm volatile (
                 "lsll %2,%0 ; setz %1"
                 : "=r" (l), "=qm" (valid) : "rm" (sel));

This is a TOCTOU race, which reads the GDT/LDT 3 separate times.

Yes, hence why it's debug-only code.

Quoted 3 lines

Nothing prevents another CPU issuing do_update_descriptor() against this descriptor, and hitting the BUG_ON() despite the care taken in this patch.  Nor is it legitimate for there to be such an interlock.

Non-malicious code isn't going to update descriptors when they may be in use on another (v)CPU.

Quoted 3 lines

I suggest we just delete the CONFIG_DEBUG section.  Because the segment registers aren't architecturally available for PV guests, this check simply cannot be made safe.

Sure, we can also delete the section, but double checking our "manual" calculations seemed like a good idea to me back at the time, and still does. Hence if you want the section removed, may I suggest that you send a patch with what you think is a suitable justification? I'm okay to ack such a patch, but I'd be less okay to make one.

Jan

#4 lore raw

PATCH 2/2 x86/PV: modernize descriptor read sanity checks

In reply to Jan Beulich

First of all use ASM_FLAG_OUT() for better codegen with new enough gcc. Then further change l's type, such that the compiler no longer has a need to emit an extra MOV ahead of the CMP: LAR zero-extends the result just fine.

---
Imo the #ifndef NDEBUG (soon to be switched to CONFIG_DEBUG) could be dropped altogether, by using ASSERT() in place of BUG_ON(), and by dropping the volatile from the asm()s. Then the compiler ought to be able to eliminate the entire if() in release builds.

xen/arch/x86/pv/emulate.c +9 -6
@@ -52,17 +52,20 @@ int pv_emul_read_descriptor(unsigned int
 #ifndef NDEBUG
         if ( sel > 3 )
         {
-            unsigned int a, l;
-            unsigned char valid;
+            unsigned int a;
+            unsigned long l;
+            bool valid;
 
             asm volatile (
-                "larl %2,%0 ; setz %1"
-                : "=r" (a), "=qm" (valid) : "rm" (sel));
+                "lar %[sel], %[ar];" ASM_FLAG_OUT(, "setz %[zf]")
+                : [ar] "=r" (a), ASM_FLAG_OUT("=@ccz", [zf] "=qm") (valid)
+                : [sel] "rm" (sel));
             BUG_ON(valid && ((a ^ desc.b) & 0x00f0ff00));
 
             asm volatile (
-                "lsll %2,%0 ; setz %1"
-                : "=r" (l), "=qm" (valid) : "rm" (sel));
+                "lsl %[sel], %k[lim];" ASM_FLAG_OUT(, "setz %[zf]")
+                : [lim] "=r" (l), ASM_FLAG_OUT("=@ccz", [zf] "=qm") (valid)
+                : [sel] "rm" (sel));
             BUG_ON(valid && (l != *limit));
         }
 #endif
#5 lore raw

Re: [PATCH 2/2] x86/PV: modernize descriptor read sanity checks

Quoted 11 lines · Jan Beulich

First of all use ASM_FLAG_OUT() for better codegen with new enough gcc. Then further change l's type, such that the compiler no longer has a need to emit an extra MOV ahead of the CMP: LAR zero-extends the result just fine.

---
Imo the #ifndef NDEBUG (soon to be switched to CONFIG_DEBUG) could be dropped altogether, by using ASSERT() in place of BUG_ON(), and by dropping the volatile from the asm()s. Then the compiler ought to be able to eliminate the entire if() in release builds.

I've committed the swap to CONFIG_DEBUG, but as per my reply to patch 1, I think the section needs deleting.

~Andrew