Skip to contentSkip to conversation

add canonical address checks to HVM

PATCH In review Jan Beulich 2 participants 9 messages Nov 29, 2006 – Dec 1, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 add canonical address checks to HVM

Add proper long mode canonical address checks to PIO emulation and MSR writes, the former paralleling the limit checks added for 32-bit guests. Also catches two more cases in the MSR handling code where only ECX (rather than RCX) should be used.

xen/arch/x86/hvm/svm/emulate.c +0 -11
Index: 2006-11-27/xen/arch/x86/hvm/svm/emulate.c
===================================================================
@@ -128,17 +128,6 @@ static inline unsigned long DECODE_GPR_V
         return (unsigned long) -1; \
     }
 
-#if 0
-/*
- * hv_is_canonical - checks if the given address is canonical
- */
-static inline u64 hv_is_canonical(u64 addr)
-{
-    u64 bits = addr & (u64)0xffff800000000000;
-    return (u64)((bits == (u64)0xffff800000000000) || (bits == (u64)0x0));
-}
-#endif
-
 #define modrm operand [0]
 
 #define sib operand [1]
xen/arch/x86/hvm/svm/svm.c +78 -26

177 lines not on this page · raw message

xen/arch/x86/hvm/vmx/vmx.c +66 -32

176 lines not on this page · raw message

xen/include/asm-x86/hvm/io.h +6 -0
Index: 2006-11-27/xen/include/asm-x86/hvm/io.h
===================================================================
@@ -25,6 +25,12 @@
 #include <public/hvm/ioreq.h>
 #include <public/event_channel.h>
 
+#ifdef __x86_64__
+#define IS_CANO_ADDRESS(add) (((long)(add) >> 47) == ((long)(add) >> 63))
+#else
+#define IS_CANO_ADDRESS(add) 1
+#endif
+
 #define operand_size(operand)   \
     ((operand >> 24) & 0xFF)
#1 lore raw
Quoted 6 lines · Jan Beulich

Add proper long mode canonical address checks to PIO emulation and MSR writes, the former paralleling the limit checks added for 32-bit guests. Also catches two more cases in the MSR handling code where only ECX (rather than RCX) should be used.

I wonder if we would be better consistently *removing* the canonical-address checks? It's not a security issue after all -- the check is done in hardware only to prevent code from ever depending on being able to use the high address bits for software flags. I think it is harmless to deviate from native behaviour on this issue and makes our emulation code smaller and simpler.

-- Keir

#2 lore raw
Quoted 8 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 30.11.06 18:55 >>>

Quoted 5 lines · Jan Beulich

Add proper long mode canonical address checks to PIO emulation and MSR writes, the former paralleling the limit checks added for 32-bit guests. Also catches two more cases in the MSR handling code where only ECX (rather than RCX) should be used.

Quoted 8 lines
Quoted 1 line

I wonder if we would be better consistently *removing* the canonical-address checks? It's not a security issue after all -- the check is done in hardware only to prevent code from ever depending on being able to use the high address bits for software flags. I think it is harmless to deviate from native behaviour on this issue and makes our emulation code smaller and simpler.

I think it might be a security issue:
- In MSR writes, are you certain there's not going to be any problem now or in the future when the state gets actually loaded into CPU registers?
- In memory accesses, at least until no failures to read/write guest memory are being ignored anymore.

Jan

#3 lore raw
Quoted 5 lines · Jan Beulich

I think it might be a security issue:
- In MSR writes, are you certain there's not going to be any problem now or in the future when the state gets actually loaded into CPU registers?
- In memory accesses, at least until no failures to read/write guest memory are being ignored anymore.

We should be defensive about guest reads/writes/MSR-accesses anyway. I.e., we should at least accept faults on those accesses, and make sure the worst that happens is a domain crash.

-- Keir

#4 lore raw
Quoted 9 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 01.12.06 09:07 >>>

Quoted 5 lines · Jan Beulich

I think it might be a security issue:
- In MSR writes, are you certain there's not going to be any problem now or in the future when the state gets actually loaded into CPU registers?
- In memory accesses, at least until no failures to read/write guest memory are being ignored anymore.

Quoted 3 lines

We should be defensive about guest reads/writes/MSR-accesses anyway. I.e., we should at least accept faults on those accesses, and make sure the worst that happens is a domain crash.

That I take for granted. But it's far from optimal. I don't know about modern Windows (has been too long since I was last looking at their handling of this), but at least Linux takes precautions when doing potentially dangerous accesses in so many places that it would seem unreasonable to crash a domain when it could be passed a simple fault at the right point, and let it decide for itself whether it wants to die.

Jan

#5 lore raw
Quoted 11 lines · Jan Beulich
Quoted 3 lines

We should be defensive about guest reads/writes/MSR-accesses anyway. I.e., we should at least accept faults on those accesses, and make sure the worst that happens is a domain crash.

That I take for granted. But it's far from optimal. I don't know about modern Windows (has been too long since I was last looking at their handling of this),
but at least Linux takes precautions when doing potentially dangerous accesses in so many places that it would seem unreasonable to crash a domain when it could be passed a simple fault at the right point, and let it decide for itself whether it wants to die.

Linux would never be barking mad enough to poke a non-canonical address in 64-bit mode, I'm sure. :-) But yes, I will take a closer look at the patch...

-- Keir

#6 lore raw
In reply to Jan Beulich

xen/include/asm-x86/hvm/io.h line 33

Quoted 6 lines · Jan Beulich
xen/include/asm-x86/hvm/io.h +6 -0
+#ifdef __x86_64__
+#define IS_CANO_ADDRESS(add) (((long)(add) >> 47) == ((long)(add) >> 63))
+#else
+#define IS_CANO_ADDRESS(add) 1
+#endif
+

Is there any guarantee that right-shift is signed when using gcc?

How about (int16_t)((add) >> 48) == -(int)(((add) >> 47) & 1) ??

-- Keir

#7 lore raw
Quoted 11 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 01.12.06 11:12 >>>

xen/include/asm-x86/hvm/io.h line 33

Quoted 6 lines · Jan Beulich
xen/include/asm-x86/hvm/io.h +6 -0
+#ifdef __x86_64__
+#define IS_CANO_ADDRESS(add) (((long)(add) >> 47) == ((long)(add) >> 63))
+#else
+#define IS_CANO_ADDRESS(add) 1
+#endif
+

Is there any guarantee that right-shift is signed when using gcc?

I suppose so, I believe this is assumed to be that way in various other places. However, I'm not sure I have an idea where I could look up implementation defined behavior for gcc.

Quoted 1 line

How about (int16_t)((add) >> 48) == -(int)(((add) >> 47) & 1) ??

Sure, should work too, but would incur more overhead. I was actually trying to even avoid the two shifts, but I wasn't able to find something that would use just one *and* would be faster than the version I submitted.

Jan

#8 lore raw
Quoted 7 lines
Quoted 2 lines

Is there any guarantee that right-shift is signed when using gcc?

I suppose so, I believe this is assumed to be that way in various other places.
However, I'm not sure I have an idea where I could look up implementation defined behavior for gcc.

Looking into it a bit I think a compiler has to be consistent (i.e., it is implementation *defined*) and gcc makes reasonable effort to do signed shifts on architectures that have ISA support for it. I may add a boot-time BUG_ON() just as a sanity check. :-)

Quoted 3 lines

Sure, should work too, but would incur more overhead. I was actually trying to even avoid the two shifts, but I wasn't able to find something that would use just one *and* would be faster than the version I submitted.

I don't think it's possible. Two shifts and a compare is pretty tight.

-- Keir

Quoted 7 lines

Jan

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