Skip to contentSkip to conversation

x86: possible problem with guest_walk_tables()

RFC In review Jan Beulich 2 participants 7 messages Mar 21, 2011 – Mar 28, 2011

lore
Share & tools
lore
#0 lore raw

RFC 1/1 x86: possible problem with guest_walk_tables()

The PSE handling doesn't check bits 1...8 (or 1...9 for non-PAE guests) being zero, thus allowing bad (not 2Mb/4Mb aligned) large pages to be handled (afaict potentially allowing the guest to access foreign memory). Below is a possible fix, but unfortunately it doesn't work for GUEST_PAGING_LEVELS == 2, since _PAGE_INVALID_BITS is zero there. Would defining _PAGE_INVALID_BITS to any bit mask between 0x80000000 and 0xfffff000 there be in conflict with anything?

Jan

xen/arch/x86/mm/guest_walk.c +8 -2
@@ -231,9 +231,15 @@ guest_walk_tables(struct vcpu *v, struct
             /* _PAGE_PSE_PAT not set: remove _PAGE_PAT from flags. */
             flags &= ~_PAGE_PAT;
 
+#define GUEST_L2_GFN_ALIGN (1 << (GUEST_L2_PAGETABLE_SHIFT - \
+                                  GUEST_L1_PAGETABLE_SHIFT))
+        if ( gfn_x(start) & (GUEST_L2_GFN_ALIGN - 1) & ~0x1 )
+            rc |= _PAGE_INVALID_BITS;
+
         /* Increment the pfn by the right number of 4k pages.  
-         * The ~0x1 is to mask out the PAT bit mentioned above. */
-        start = _gfn((gfn_x(start) & ~0x1) + guest_l1_table_offset(va));
+         * Mask out PAT and invalid bits. */
+        start = _gfn((gfn_x(start) & ~(GUEST_L2_GFN_ALIGN - 1)) +
+                     guest_l1_table_offset(va));
         gw->l1e = guest_l1e_from_gfn(start, flags);
         gw->l1mfn = _mfn(INVALID_MFN);
     } 
#1 lore raw
Quoted 3 lines · Jan Beulich

The PSE handling doesn't check bits 1...8 (or 1...9 for non-PAE guests) being zero, thus allowing bad (not 2Mb/4Mb aligned) large pages to be handled (afaict potentially allowing the guest to access foreign memory).

I don't think this allows access to foreign memory, since these pagetables are in GFN-space.

Quoted 3 lines

Below is a possible fix, but unfortunately it doesn't work for GUEST_PAGING_LEVELS == 2, since _PAGE_INVALID_BITS is zero there.

What does real hardware do in this case? IIRC 32-bit non-PAE doesn't have an "invlid bit set" error code to use in pagefaults. /me supposes he had better check...

Tim.

Quoted 30 lines

Would defining _PAGE_INVALID_BITS to any bit mask between 0x80000000 and 0xfffff000 there be in conflict with anything?

xen/arch/x86/mm/guest_walk.c +8 -2
@@ -231,9 +231,15 @@ guest_walk_tables(struct vcpu *v, struct
             /* _PAGE_PSE_PAT not set: remove _PAGE_PAT from flags. */
             flags &= ~_PAGE_PAT;
 
+#define GUEST_L2_GFN_ALIGN (1 << (GUEST_L2_PAGETABLE_SHIFT - \
+                                  GUEST_L1_PAGETABLE_SHIFT))
+        if ( gfn_x(start) & (GUEST_L2_GFN_ALIGN - 1) & ~0x1 )
+            rc |= _PAGE_INVALID_BITS;
+
         /* Increment the pfn by the right number of 4k pages.  
-         * The ~0x1 is to mask out the PAT bit mentioned above. */
-        start = _gfn((gfn_x(start) & ~0x1) + guest_l1_table_offset(va));
+         * Mask out PAT and invalid bits. */
+        start = _gfn((gfn_x(start) & ~(GUEST_L2_GFN_ALIGN - 1)) +
+                     guest_l1_table_offset(va));
         gw->l1e = guest_l1e_from_gfn(start, flags);
         gw->l1mfn = _mfn(INVALID_MFN);
     } 

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

Signature · 3 lines
Tim Deegan <[email protected]>
Principal Software Engineer, Xen Platform Team
Citrix Systems UK Ltd.  (Company #02937203, SL9 0BG)
#2 lore raw
Quoted 8 lines
Quoted 1 line
Quoted 1 line · Tim Deegan
Quoted 3 lines · Jan Beulich

The PSE handling doesn't check bits 1...8 (or 1...9 for non-PAE guests) being zero, thus allowing bad (not 2Mb/4Mb aligned) large pages to be handled (afaict potentially allowing the guest to access foreign memory).

I don't think this allows access to foreign memory, since these pagetables are in GFN-space.

Yes, if this is really only GFN space, then it would "just" result in bad translations getting installed, possibly conflicting with others (e.g. in cache attributes).

Quoted 7 lines
Quoted 3 lines

Below is a possible fix, but unfortunately it doesn't work for GUEST_PAGING_LEVELS == 2, since _PAGE_INVALID_BITS is zero there.

What does real hardware do in this case? IIRC 32-bit non-PAE doesn't have an "invlid bit set" error code to use in pagefaults. /me supposes he had better check...

The bits are marked reserved (minus the PSE-36 feature), and the manual doesn't make any distinction between the various paging modes when it comes to error code bits other than bit 4. I therefore think that on newer CPUs you would see bit 3 set in this case (minus errata). Hence I think forcing a page fault in this case would be correct.

Leaving aside the non-PAE case, does the fix presented look reasonable?

Jan

#3 lore raw
Quoted 3 lines · Jan Beulich

Yes, if this is really only GFN space, then it would "just" result in bad translations getting installed, possibly conflicting with others (e.g. in cache attributes).

Yep; that's no worse that anything else a guest can do. I'm pretty sure no guest installs unaligned PAE entries and then tries to use them. (The only case I _know_ of where the OS relies on h/w to enforce invalid-bit detection is Xen itself).

Quoted 2 lines

Leaving aside the non-PAE case, does the fix presented look reasonable?

Yes, it looks correct to me. Give me a Signed-off-by and I'll apply it.

Thanks,

Tim.

Signature · 3 lines
Tim Deegan <[email protected]>
Principal Software Engineer, Xen Platform Team
Citrix Systems UK Ltd.  (Company #02937203, SL9 0BG)
#4 lore raw
Quoted 10 lines
Quoted 1 line
Quoted 1 line · Tim Deegan
Quoted 3 lines · Jan Beulich

Yes, if this is really only GFN space, then it would "just" result in bad translations getting installed, possibly conflicting with others (e.g. in cache attributes).

Yep; that's no worse that anything else a guest can do. I'm pretty sure no guest installs unaligned PAE entries and then tries to use them. (The only case I _know_ of where the OS relies on h/w to enforce invalid-bit detection is Xen itself).

... and luckily Xen itself no longer runs on non-PAE (so this doesn't become an issue with nested virtualization).

Quoted 4 lines
Quoted 2 lines

Leaving aside the non-PAE case, does the fix presented look reasonable?

Yes, it looks correct to me. Give me a Signed-off-by and I'll apply it.

Going with the page-fault-less case then for non-PAE? If we want that case to produce reserved bit faults too, I admit I'd prefer to submit a complete patch (which is half the reason why this was an RFC, non-signed-off one)... If we want no fault there, I'd like to comment that way in the code (so the or-ing with zero won't prompt later readers to wonder whether this isn't a bug/oversight).

Jan

#5 lore raw
Quoted 11 lines · Jan Beulich
Quoted 4 lines
Quoted 2 lines

Leaving aside the non-PAE case, does the fix presented look reasonable?

Yes, it looks correct to me. Give me a Signed-off-by and I'll apply it.

Going with the page-fault-less case then for non-PAE? If we want that case to produce reserved bit faults too, I admit I'd prefer to submit a complete patch (which is half the reason why this was an RFC, non-signed-off one)... If we want no fault there, I'd like to comment that way in the code (so the or-ing with zero won't prompt later readers to wonder whether this isn't a bug/oversight).

OK, I'll wait until I can fix up the non-PAE case too.

Tim.

Signature · 3 lines
Tim Deegan <[email protected]>
Principal Software Engineer, Xen Platform Team
Citrix Systems UK Ltd.  (Company #02937203, SL9 0BG)
#6 lore raw
Quoted 7 lines
Quoted 1 line
Quoted 1 line · Tim Deegan
Quoted 5 lines · Jan Beulich
Quoted 4 lines
Quoted 2 lines

Leaving aside the non-PAE case, does the fix presented look reasonable?

Yes, it looks correct to me. Give me a Signed-off-by and I'll apply it.

Quoted 8 lines
Quoted 6 lines

Going with the page-fault-less case then for non-PAE? If we want that case to produce reserved bit faults too, I admit I'd prefer to submit a complete patch (which is half the reason why this was an RFC, non-signed-off one)... If we want no fault there, I'd like to comment that way in the code (so the or-ing with zero won't prompt later readers to wonder whether this isn't a bug/oversight).

OK, I'll wait until I can fix up the non-PAE case too.

Jun points out that SDM tables 5-8 and 5-9 are pretty complete. According to them, only bit 21 set would cause a reserved bit fault. Which fits pretty badly into the present code, hence I'm not certain it's worth to uglify the code just for this case. I'll therefore submit the patch with just a comment added for now - later adjustments can still be done if desired.

Jan